c_hash

Table Of Contents

Introduction, c_hash

Like c_map, c_hash is an associative collection class. Unlike c_map, c_hash does not store it's elements pre-sorted. c_hash stores it's elemnts in what appears to be a random order, yet still provides fast key-value insertion, and key-value retreival, In fact, insertion and retrival is significantly faster than with c_map.

As with c_map, c_has also uses c_map_pair for insertion and retreival. This is so it will conform to the interface for c_collection.

Example Code:
void dea_search_and_seisure( void )
{
  c_person bob("Bob");
  c_hash<c_string,c_string> bobs_car;
  c_string not_here("no drugs"), here_they_are("drugs");

  bobs_car.add( c_string("the radio"),        not_here );
  bobs_car.add( c_string("the trunk"),        not_here );
  bobs_car.add( c_string("the gas tank"),     not_here );
  bobs_car.add( c_string("under the seat"),   not_here );

  if( has_drugs( bobs_car ) ) {
    cout << "you have the right to remain silent..." << endl;
  }
  else {
    cout << "you got lucky...this time." << endl;
  }
}

void plant_evidence( c_hash<c_string,c_string>& vehicle ) 
{
  vehicle.add( c_string("the tires"), c_string("drugs") );
}

int has_drugs( c_hash<c_string,c_string>& vehicle ) 
{
  c_map_pair<c_string,c_string> hiding_place;
  c_string has_drugs("drugs");

  hiding_place.m_key = "the radio";
  vehicle.find( hiding_place );
  if( hiding_place.m_value == drugs ) {
    return TRUE;
  }

  hiding_place.m_key = "under the seat";
  vehicle.find( hiding_place );
  if( hiding_place.m_value == drugs ) {
    return TRUE;
  }

  hiding_place.m_key = "the tires";
  vehicle.find( hiding_place );
  if( hiding_place.m_value == drugs ) {
    return TRUE;
  }

  return FALSE;
}

Up