I'm looking for a HashTable or Dictionary implementation in C++ that has similar functionality to the one in C#? Does the STL contain an object like this and how would I use it?
Actually, to be exactly the same as .NET's Dictionary/Hashtable, what you want is hash_map or unordered_map (std::map
is implemented as a binary tree), hash_map
is an extension to the SC++L. I believe C++0x is going to include unordered_map
. Most compilers that I know of come with hash_map
, though, and boost obviously has unordered_map
until C++0x is available in all compilers, so you should just be able to use it without trouble.
The STL std::map
can be used to build a dictionary. std::map
is usually implemented as a search tree, not a hash table. That means both lookup and insertion has different perfomance characteristics than C#'s HashMap
- for very large maps, average lookup will be slower, especially if the objects in the map are fragmented in memory.
In the TR1 of the new c++ standard, you have std::tr1::unordered_map
and std::tr1::unordered_multimap
, which will usually be implemented using a hash table. If your compiler does not provide those libraries, you can use the implementation from http://www.boost.org/.
Yet another alternative is Google's sparse_hash
.