unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value。

不同的是unordered_map不会根据key的大小进行排序,存储时是根据key的hash值判断元素是否相同,即unordered_map内部元素(key)是无序的,而map中的元素是按照二叉搜索树存储,进行中序遍历会得到有序遍历。

所以使用时map的key需要定义operator<。而unordered_map需要定义hash_value函数并且重载operator==。但是很多系统内置的数据类型都自带这些,那么如果是自定义类型,那么就需要自己重载operator<或者 hash_value()了。

结论:如果需要内部元素自动排序,使用map,不需要排序使用unordered_map。

C++11为什么不把unordered_map定义为hash_map呢?那是因为在新标准出现之前很多库厂商已经暂用了hash_map这个名词。因此为了向前兼容不得不定义新的unordered_map。

所以unordered_map就是一个hash_table,类似一个用数组模拟的hash_table。但是要注意两个经常使用的函数:

find函数,在查找失败的时候返回map.end(),最后一个元素后面的一个仅仅起到标志作用的元素。

iterator find ( const key_type& k );
const_iterator find ( const key_type& k ) const;
Get iterator to element
Searches the container for an element with k as key and returns an iterator to it if found,
otherwise it returns an iterator to unordered_map::end (the element past the end of the container).

operator[]访问函数,这个可以根据key操作对应的value,跟数组模拟的hash_table使用一样。

mapped_type& operator[] ( const key_type& k );
mapped_type& operator[] ( key_type&& k );
Access element
If k matches the key of an element in the container,
the function returns a reference to its mapped value. If k does not match the key of any element in the container,
the function inserts a new element with that key and returns a reference to its mapped value.
Notice that this always increases the container size by one, even if no mapped value is assigned to the element
(the element is constructed using its default constructor).

  

  

[C++]unordered_map的使用的更多相关文章

  1. STL: unordered_map 自定义键值使用

    使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...

  2. C++11 新特性: unordered_map 与 map 的对比

    unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value.不同的是unordered_map不会根据key的大小进行排序, 存储时是根据key的ha ...

  3. C++ - unordered_map 源码解析

    转自:http://zrj.me/archives/1248,转载请注明.(分析得不错) 主要尝试回答下面几个问题: 一般情况下,使用 hash 结构,需要有桶的概念,那么 unordered_map ...

  4. map 与 unordered_map

    两者效率对比: #include <iostream> #include <string> #include <map> #include <unordere ...

  5. c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑

    转自:http://blog.csdn.net/truexf/article/details/17303263 一.vector vector采用一段连续的内存来存储其元素,向vector添加元素的时 ...

  6. C++ unordered_map remove 实现哈希表移除

    使用C++的unordered_map类型时,我们经常要根据关键字查找,并移除一组映射,在Java中直接用remove即可,而STL中居然没有实现remove这个函数,还要自己写循环来查找要删除项,然 ...

  7. map,hash_map和unordered_map 实现比较

    map介绍 Map是STL[1]的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处 ...

  8. hash_map vs unordered_map vs map vs unordered_set

    hash_map vs unordered_map 这两个的内部结构都是采用哈希表来实现.unordered_map在C++11的时候被引入标准库了,而hash_map没有,所以建议还是使用unord ...

  9. C++11中新特性之:unordered_map

    unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value. 不同的是unordered_map不会根据key的大小进行排序,存储时是根据key的ha ...

  10. STL set multiset map multimap unordered_set unordered_map example

    I decide to write to my blogs in English. When I meet something hard to depict, I'll add some Chines ...

随机推荐

  1. Time.deltaTime和Time.realtimeSinceStartup

    private float f = 0f;void Update () {      f += Time.deltaTime;      Debug.LogError ("Time.delt ...

  2. 解决jQuery.live在mobile safari(iphone / ipad / ipod)绑定失败的问题

    解决方案: 给要使用live绑定事件的元素,添加“cursor:pointer”样式即可! 如: a,input,td{cursor:pointer;}

  3. javascript设计模式——Module

    Module模式是提供公有和私有方法的代码块,有利于封装组织代码,可减少变量及函数名与其它模块的冲突. 推荐阅读: http://www.adequatelygood.com/JavaScript-M ...

  4. asp.net 2.0 Session丢失问题

    可行的解决方法(本人已用): 1.Web.config文件修改sessionstate模式(默认为InProc) <sessionState mode="/> 2.开启ASP.N ...

  5. C++结构体中sizeof(1)

    sizeof sizeof操作符的作用是返回一个对象或类型名的长度,长度的单位是字节. 返回值的类型是标准库命名为size_t的类型,size_t类型定义在cstddef头文件中,该头文件是C标准库的 ...

  6. Fedora 开启 ssh

    Fedora 17 已经安装好openssh server了 不用再装 不过默认无开启 首先su root1.开启ssh服务# systemctl start sshd.service 2.随系统一起 ...

  7. JavaScript、jQuery获取页面及个元素高度、宽度

    Javascript获取获取屏幕.浏览器窗口 ,浏览器,网页高度.宽度的大小网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.client ...

  8. HTML5中的服务器‘推送’技术 -Server-Sent Events

    转帖:http://www.developersky.net/thread-63-1-1.html 一直以来,HTTP协议都是严格遵循Request-Response模型的.客户端发送一个Reques ...

  9. Deep Learning for Natural Language Processing1

    Focus, Follow, and Forward Stanford CS224d 课程笔记 Lecture1 Stanford CS224d 课程笔记 Lecture1 Stanford大学在20 ...

  10. 怎查看linux系统的位数

    # uname -a x86_64则说明你是64位内核, 跑的是64位的系统. i386, i686说明你是32位的内核, 跑的是32位的系统