Hash Table v/s STL map in C++

故事扮演 提交于 2019-11-30 02:46:14

问题


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?


回答1:


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




回答2:


  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.




回答3:


The key-value pairs are stored in an std::pair. Its a templated struct; an element called first stores the key, and an element called second stores the value. Some info.



来源:https://stackoverflow.com/questions/2460387/hash-table-v-s-stl-map-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!