C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 12th, 2003, 02:14 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
Tip about STL hash_map and string

This is not really a question, but a tip for others who may possibly search the forum for this particular issue:

The problem:
This code works:
Code:
#include <iostream>
#include <hash_map>
using namespace std;

hash_map <const char *, int> months;
int main(void) {
   months["january"] = 31;
   months["february"] = 28;
   return 0;
}


However, this code doesn't compile:
Code:
#include <iostream>
#include <string>
#include <hash_map>
using namespace std;

hash_map <string, int> months;
int main(void) {
   months["january"] = 31;
   months["february"] = 28;
   return 0;
}

Note that the only difference between the two codes is that one uses const char * and the other uses string for the hash key type. So why does the second instance not compile?

Background: What is a hash_map?
The STL library provides a template type called map which allows a C++ programmer to create an associative array (i.e). array[key] = value;
However, a map template automatically orders its elements as they're inserted, therefore lookups are a little slower. A hash_map is a template type that allows a programmer to quickly look up a value. The hash_map template uses a hashing algorithm to store values that are inserted into it, hence the elements are NOT sorted. This is OK, if you just want to do quick in-memory lookups, but don't care about the data being sorted. The map type is part of the STL, but hash_map was introduced by SGI and is present in only some implementations of the STL (M$ VC++ 6.0 and .NET do not have it in their default installation). However, two of the well known implementations from SGI (http://www.sgi.com/tech/stl/index.html) and stlport (http://www.stlport.org/) do and can be used as drop in replacements, if your compiler does not support hash maps. See http://www.sgi.com/tech/stl/table_of_contents.html for documentation and sample code using hash_map .

The reason it doesn't compile:
Since hash_map is not part of the standard STL, there are certain other parts of the STL that it doesn't work with (notably the string template, which is pretty much standard across all STL implementations). You need to supply some extra code yourself, for the hash function to work correctly with other parts of the STL, which are standard across all implementations.

The solution:
Add the following chunk of code between the BEGIN FIX and END FIX comments:
Code:
#include <iostream>
#include <string>
#include <hash_map>
using namespace std;

/** BEGIN FIX **/
namespace std                                                                                 
{                                                                                             
  template<> struct hash< std::string >                                                       
  {                                                                                           
    size_t operator()( const std::string& x ) const                                           
    {                                                                                         
      return hash< const char* >()( x.c_str() );                                              
    }                                                                                         
  };                                                                                          
}          
/** END FIX **/

hash_map <string, int> months;
int main(void) {
   months["january"] = 31;
   months["february"] = 28;
   return 0;
}


I tested this code on the following OS/compiler environments:
Red Hat Linux 7.1: gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81)
FreeBSD 4.7-RELEASE-p7:gcc version 2.95.4 20020320 [FreeBSD]
OpenBSD 3.3-CURRENT:gcc version 2.95.3 20010125 (prerelease, propolice)

[edit] Note that I was using the STL implementations that came by default with the distros and they all supported hash_map [/edit]

Hope this helps!

Last edited by Scorpions4ever : March 12th, 2003 at 06:11 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Tip about STL hash_map and string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway