views:

202

answers:

6

I need to create a hash table that has a key as a string, and value as an int. I cannot use STL containers on my target. Is there a suitable hash table class for this purpose?

A: 

You can use the unordered associative container from Boost, aka. boost::unordered_map, which is implemented in terms of a hash table.

Konrad Rudolph
although it would be interesting what the poster's opposition to STL is. If it is the fact that templates are used then boost is out as well.
mjmarsh
@mjmarsh: True but in that case a lot more information would have been helpful. Even excluding the STL is an unreasonable request as far as I am concerned but since no other restrictions were mentioned, I think using the Boost libraries are probably the obvious choice.
Konrad Rudolph
A: 

It's a moot point since STL has no hash table container; std::map would be the alternative. For most purposes there is no reason not to use std::map. For uses that require a hashtable, boost::unordered_map is the best choice (and I think matches the hashtable defined in the new C++ TR1 proposed standard. Some compilers -- but I can't name them -- may provide the TR1 hashtable as std::tr1::unordered_map

c-urchin
A: 

You might want to check out glib hash tables
http://library.gnome.org/devel/glib/stable/glib-Hash-Tables.html

frankc
+1  A: 

In the case that you know your list of keys ahead of time (or some superset thereof), you can use a perfect hash function generator like gperf. gperf will spit out either C or C++ code.

(You may need to do some work to actually build a container, given the hash function, though.)

leander
+1  A: 
Michael Dorgan
A: 

If you need maximum performance, use MCT's closed_hash_map or Google's dense_hash_map. The former is easier to use, the latter is more mature. Your use case sounds like it would benefit from closed hashing.

doublep