Skip to content

Instantly share code, notes, and snippets.

@liuguiyangnwpu
Last active August 4, 2016 04:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuguiyangnwpu/b1006e4e845f0f003f0d346d66123fa8 to your computer and use it in GitHub Desktop.
Save liuguiyangnwpu/b1006e4e845f0f003f0d346d66123fa8 to your computer and use it in GitHub Desktop.
使用STL中的Map和Hash_Map和Unordered_Map的底层细节区别
  • unordered_map在迭代器遍历时候key,value出现的顺序是未定义
  • 在STL中的Map底层的实现是红黑树,其插入和查找效率较低,对于插入和查找效率较高的HashMapC11推荐使用unordered_map
  • 在MSDN中又这样的描述

Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).

unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

这段英文按我的理解就是unordered_map的快速插入和快速查找都是没有问题的,只不过对于迭代器遍历的速度恐怕会相比低效一些。

  • map的迭代输出是树的中序遍历,因此保证有序,且效率还行。 而事实上unordered_map直接使用了开链的hashtable而已,因为迭代效率低下。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment