I am trying to learn C++ maps. Was just wondering about the implementation of STL map. I read it employs Binary search tree.
Is there a implementation of hash table in STL?
How exactly do STL map stores Key Value pairs?
map uses a red-black tree as the data structure, so the elements you put in there are sorted, and insert/delete is O(log(n)). The elements need to implement at least operator< . hashmap uses a hash, so elements are unsorted, insert/delete is O(1).
Hashmap vs HashtableIt is not thread-safe and can't be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads. HashMap allows one null key and multiple null values whereas Hashtable doesn't allow any null key or value.
In C++, the sorted map (std::map) is usually implemented as a binary tree, and the unsorted map (std::unordered_map) is a hash table with closed addressing. A hash table can deliver O(1) lookup time, whereas a binary tree has O(log n) lookup.
Typical STL implementations are based on Red-Black trees. C++ TR1 provides std::tr1::unordered_map which uses a hash table implementation. Boost also provides an unordered_map hash table implementation.
C++11 now has std::unordered_map
Some libraries implement stdext::hash_map
which has almost the same interface as std::map
but uses a hash table instead of a binary tree.
The binary tree nodes are arranged in the tree according the key, and each key has a value attached, either in whole in the same node, or as a pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With