cam
cam

Reputation: 9033

Dictionary/HashTable Object in C++?

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?

Upvotes: 13

Views: 21068

Answers (4)

spoulson
spoulson

Reputation: 21591

STL has std::map    

Upvotes: 5

Dean Harding
Dean Harding

Reputation: 72658

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. 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.

Upvotes: 11

gnud
gnud

Reputation: 78518

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.

Upvotes: 2

I believe you're looking for map. See here for more.

Upvotes: 2

Related Questions