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. 告示:CSDN博客通道支持Windows Live Writer写blog离线好友

    尊敬的各位CSDN用户: 您好! 为了更好的服务客户.CSDN已经支持Windows Live Writer离线写博客啦.Windows Live Writer于2014年5月29日正式上线啦!欢迎大 ...

  2. 求职(2015南京站获得百度、美的集团、趋势科技、华为offer)

    版权所有所有:没有马缰绳chhuach(CSDN博客源).转载请注明出处. 禁止www.haogongju.net转载. 特此声明 一.开篇: 9月底,找工作接近尾声,笔者主要经历了2015年南京站百 ...

  3. js 创建类和继承的几种方法

    在面向对象编程中,类(class)是对象(object)的模板,定义了同一组对象(又称"实例")共有的属性和方法.JavaScript语言里是没有类的概念的,但是我们通过以下方法也 ...

  4. node.js url模块

    URL url.parse(urlStr[, parseQueryString][, slashesDenoteHost]) url.format(urlObj) url.resolve(from, ...

  5. SQL中DATEPART返回日期单独部分

    定义和用法 DATEPART() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 DATEPART(datepart,date) date 参数是合法的日期表达式.datep ...

  6. tabbedApliction

    一.手动创建UITabBarController 最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLa ...

  7. JavaScript中的this引用

    在JavaScript的学习当中,this关键字的出现频率可不低,所以想想有必要对this关键字做一个总结.在总结过程中,参考的资料来源于书本及网上. 一.定义 1.this是函数内部的一个特殊对象( ...

  8. python中调用C++写的动态库

    一.环境:Windows XP + Python3.2 1. dll对应的源文件(m.cpp): #include <stdio.h> extern "C" { _de ...

  9. PARTITION(number theory) ALSO Explosive number in codewars

    问题出于codewars,简言之:寻找和为一个整数的的不同整数组合.https://en.wikipedia.org/wiki/Partition_(number_theory) 例如:和为6的整数组 ...

  10. locate linux文件查找命令

    locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...