Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash Table v/s STL map in C++

I am trying to learn C++ maps. Was just wondering about the implementation of STL map. I read it employs Binary search tree.

  1. Is there a implementation of hash table in STL?

  2. How exactly do STL map stores Key Value pairs?

like image 747
anon Avatar asked Mar 17 '10 07:03

anon


People also ask

Is a hash map the same as a map C++?

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

Is hash table and map same?

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.

Is std::map a Hashtable?

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.


2 Answers

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

like image 55
Will Avatar answered Sep 17 '22 15:09

Will


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

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

like image 42
Max Shawabkeh Avatar answered Sep 17 '22 15:09

Max Shawabkeh