[C++]unordered_map的使用
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的使用的更多相关文章
- STL: unordered_map 自定义键值使用
使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...
- C++11 新特性: unordered_map 与 map 的对比
unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value.不同的是unordered_map不会根据key的大小进行排序, 存储时是根据key的ha ...
- C++ - unordered_map 源码解析
转自:http://zrj.me/archives/1248,转载请注明.(分析得不错) 主要尝试回答下面几个问题: 一般情况下,使用 hash 结构,需要有桶的概念,那么 unordered_map ...
- map 与 unordered_map
两者效率对比: #include <iostream> #include <string> #include <map> #include <unordere ...
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
转自:http://blog.csdn.net/truexf/article/details/17303263 一.vector vector采用一段连续的内存来存储其元素,向vector添加元素的时 ...
- C++ unordered_map remove 实现哈希表移除
使用C++的unordered_map类型时,我们经常要根据关键字查找,并移除一组映射,在Java中直接用remove即可,而STL中居然没有实现remove这个函数,还要自己写循环来查找要删除项,然 ...
- map,hash_map和unordered_map 实现比较
map介绍 Map是STL[1]的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处 ...
- hash_map vs unordered_map vs map vs unordered_set
hash_map vs unordered_map 这两个的内部结构都是采用哈希表来实现.unordered_map在C++11的时候被引入标准库了,而hash_map没有,所以建议还是使用unord ...
- C++11中新特性之:unordered_map
unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value. 不同的是unordered_map不会根据key的大小进行排序,存储时是根据key的ha ...
- 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 ...
随机推荐
- uva 748 Exponentiation 浮点数乘方运算 高精度水题
输入的前六位数表示一个小数,然后输入一个数表示几次方.要求用高精度算出结果. 高精度水题,主要注意处理小数点,先在输入时把小数点提取出来并记录位置,用普通乘法计算出结果后由后向前计算位置添加小数点. ...
- IE下图片切换的时候,图片总是切换不成功---根本问题是IE缓存图片
作为WEB设计者,为了在网页展示上加强用户体验,经常会利用图象载入显示状态方法,这自然需要Image对象的onload事件. 在firefox浏览器下完成开发后,可是在IE浏览器中进行调试总不能被调用 ...
- sql显示12个月数据
需求 最近在做一个财务报表展示系统,Budget需要当月上传,还未上传月份的数据也需要显示出来. 数据库设计 cBudget表结构如下 CREATE TABLE [dbo].[cBudget]( ,) ...
- 2014.12.13 ASP.NET文件上传
一.文件上传:(一)上传到硬盘文件夹1.最简单的上传. [HTML代码] <asp:FileUpload ID="FileUpload1" runat="serve ...
- 简单的javascript实例一(时钟特效)
方便以后copy 时钟特效 <html> <head> <meta http-equiv="Content-Type" content="t ...
- uinavagation 透明代码
NSShadow *shadow = [[NSShadow alloc] init]; [shadow setShadowOffset:CGSizeMake(1, 1)]; [shadow setSh ...
- Inno setup卸载前退出进程、删除文件夹
[Code]function InitializeUninstall(): Boolean; var MainRun: HWND; var MVRun:HWND; begin// FindWindow ...
- 运用Hibernate-Tools自动生成Java类和schema时,出现not found while looking for property...异常
问题描述: 在使用Hibernate-tools时出现not found while looking for property...(具体信息省略). 问题分析: 我找到出错对应的hbm.xml文件, ...
- jQuery 随滚动条滚动效果 (固定版)
//侧栏随动 var rollStart = $('.feed-mail'), //滚动到此区块的时候开始随动 rollSet = $('.search,.weibo,.group,.feed-mai ...
- 利用js制作异步验证ajax方法()
如何利用js写ajax异步验证.代码如下: window.onload = function(){ var name = document.getElementById('register-name- ...