hongbin hongbin - 11 months ago 105
C++ Question

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

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?

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download