Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between hash_map and unordered_map?

I recently discovered that the implementation of the hash map in C++ will be called unordered_map.

When I looked up why they weren't just using hash_map, I discovered that apparently there are compatibility issues with the implementation of hash_map that unordered_map resolves (more about it here).

That wiki page doesn't give much more information so I wondering if anyone knew some of the issues with hash_map that unordered_map resolves.

like image 899
kidnamedlox Avatar asked Oct 29 '09 20:10

kidnamedlox


People also ask

What is the difference between Hashmap and unordered_map?

unordered_map will do your purpose as well as hashmap in java will do . unordered map will be faster on operations like insertion and deletion than map in c++ but map possesses some extra useful characteristics such as for the elements are in sorted order , we can traverse from start to finish .

What is the difference between unordered_map and unordered_set?

unordered_set only contains keys, and no values. There is no mapping from a key to a value, so no need for an operator[] . unordered_map maps a key to a value.

Is unordered map a hash map?

Internally unordered_map is implemented using Hash Table, the key provided to map are hashed into indices of a hash table that is why the performance of data structure depends on hash function a lot but on an average, the cost of search, insert and delete from the hash table is O(1). Click here for the Complete Course!

Which is better map or unordered_map?

map is used to store elements as key,value pairs in sorted order. unordered_map is used to store elements as key,value pairs in non-sorted order.


1 Answers

Since there was no hash table defined in the C++ standard library, different implementors of the standard libraries would provide a non-standard hash table often named hash_map. Because these implementations were not written following a standard they all had subtle differences in functionality and performance guarantees.

Starting with C++11 a hash table implementation has been added to the C++ standard library standard. It was decided to use an alternate name for the class to prevent collisions with these non-standard implementations and to prevent inadvertent use of the new class by developers who had hash_table in their code.

The chosen alternate name is unordered_map which really is more descriptive as it hints at the class's map interface and the unordered nature of its elements.

like image 145
Stef Avatar answered Sep 21 '22 14:09

Stef