Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why stl choose tree based map instead of hash based map?

Tags:

c++

map

stl

I'm wondering why STL's map is base on rb tree? I mean, hash-based map seems to be more efficient in inserting/deleting or even getting the value. Are there any specific considerations?

like image 446
hongbin Avatar asked Mar 04 '12 15:03

hongbin


People also ask

Which data structure is used in the implementation of the STL map?

Map: C++ Map is another commonly used STL container. The map is an ordered data structure that holds the data in an ordered or sorted form so that elements can easily be looked up in this dictionary-like data structure.

Does std::map use hashing?

Struct std::collections::HashMap. A hash map implemented with quadratic probing and SIMD lookup. By default, HashMap uses a hashing algorithm selected to provide resistance against HashDoS attacks.

Is unordered_map a hash map?

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


1 Answers

The STL originally chose both. It had a hash table and the tree-based map.

However, when it was adopted into the standard, many parts were stripped away in order to simplify the task (it was easier to talk the committee into including a smaller library, and it required less work in terms of actually specifying their behavior).

So the hash table was skipped.

However, both data structures have their advantages. In particular, a binary tree allows the contents of the map to be ordered (you can iterate over the contents of a map in sorted order, or you can ask for all elements smaller than a specific element, for example), and I can only guess that this property was considered more important than the performance advantages of a hash map.

However, in C++11, std::unordered_map is added, which is the long lost hash table. Its original omission was simply due to time pressure and, quite possibly, committee politics (keeping the library small to minimize resistance against it)

like image 62
jalf Avatar answered Nov 15 '22 12:11

jalf