C++中的unordered_map
1.简介
随着C++0x标准的确立,C++的标准库中也终于有了hash table这个东西。很久以来,STL中都只提供<map>作为存放对应关系的容器,内部通常用红黑树实现,据说原因是二叉平衡树(如红黑树)的各种操作,插入、删除、查找等,都是稳定的时间复杂度,即O(log n);但是对于hash表来说,由于无法避免re-hash所带来的性能问题,即使大多数情况下hash表的性能非常好,但是re-hash所带来的不稳定性在当时是不能容忍的。不过由于hash表的性能优势,它的使用面还是很广的,于是第三方的类库基本都提供了支持,比如MSVC中的<hash_map>和Boost中的<boost/unordered_map.hpp>。后来Boost的unordered_map被吸纳进了TR1 (C++ Technical Report 1),然后在C++0x中被最终定了标准。
2.示例代码
#include <stdio.h>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std; void main(){
unordered_map<string,int> months;
//插入数据
cout<<"insert data"<<endl;
months["january"]=;
months["february"] = ;
months["march"] = ;
months["september"] = ; //直接使用key值访问键值对,如果没有访问到,返回0
cout<<"september->"<<months["september"]<<endl;
cout<<"xx->"<<months["xx"]<<endl; typedef unordered_map<int,int> mymap;
mymap mapping;
mymap::iterator it;
mapping[]=;
mapping[]=;
const int x=;const int y=;//const是一个C语言(ANSI C)的关键字,它限定一个变量不允许被改变,产生静态作用,提高安全性。 //寻找是否存在键值对
//方法一:
cout<<"find data where key=2"<<endl;
if( mapping.find(x)!=mapping.end() ){//找到key值为2的键值对
cout<<"get data where key=2! and data="<<mapping[x]<<endl;
}
cout<<"find data where key=3"<<endl;
if( mapping.find(y)!=mapping.end() ){//找到key值为3的键值对
cout<<"get data where key=3!"<<endl;
}
//方法二:
it=mapping.find(x);
printf("find data where key=2 ? %d\n",(it==mapping.end()));
it=mapping.find(y);
printf("find data where key=3 ? %d\n",(it==mapping.end())); //遍历hash table
for( mymap::iterator iter=mapping.begin();iter!=mapping.end();iter++ ){
cout<<"key="<<iter->first<<" and value="<<iter->second<<endl;
} system("pause");
}
C++中的unordered_map的更多相关文章
- C++11中std::unordered_map的使用
unordered map is an associative container that contains key-value pairs with unique keys. Search, in ...
- unordered_map 与 map 的对比(转)
unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value.不同的是unordered_map不会根据key的大小进行排序, 存储时是根据key的ha ...
- C++ STL之unordered_map和unordered_set的使⽤
写在最前面,本文摘录于柳神笔记: unordered_map 在头⽂件 #include <unordered_map> 中, unordered_set 在头⽂件 #include &l ...
- [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用
[LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...
- STL中的隐性性能开销与副作用
1 隐性性能开销 1.1 STL容器的clear的时间复杂度不是O(1) 很多人潜意识认为STL容器中clear()成员函数的时间复杂度为常量时间复杂度O(1).原因是大家觉得对于vector而言,c ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- Java - 容器详解
一.ArrayList 长度可变数组,类似于c++ STL中的vector. 元素以线性方式连续存储,内部允许存放重复元素. 允许对元素进行随机的快速访问,但是向ArrayList中插入和删除元素的速 ...
- [cocos2d-x]深入--几个代表性的类 (续)
摘要: 此文对cocos2d-x引擎中最具代表性,最能体现框架结构的几个类做了简单的介绍, 包括Director,Application, Renderer, EventDispatcher, Sch ...
随机推荐
- collectionView关于点击事件
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)i ...
- 批处理就是windows的杰作啊
今天要为了解决vs不能同时开启调试和编写的问题,我就上网查找了一些批处理的命令,用批处理调用exe,和打开txt,虽然一行代码就解决了但是我没用过啊,很陌生. call 路径\a.exe 就相当于 ...
- HBASE学习笔记--配置信息
hbase的配置信息,在hbase-site.xml里面有详细说明. 可以按照需要查询相关的配置. <?xml version="1.0"?> <?xml-sty ...
- VMware vCenter Converter 使用,Error code: 2147549183 (0x8000FFFF) 解决方案
在实际生产环境中,为了节省成本,会将实体机转换为虚拟机,从而虚拟化. 在公司数据中心环境的中跑VMware vCenter ConverterStandalone Client 4.3,将其中一台on ...
- AngularJs 实例
1.AngularJs 表单验证: 示例 .controller('signupController', ['$scope', function($scope) { $scope.submitted ...
- Tomcat7配置数据源(Oracle)
修改../conf/content.xml <?xml version='1.0' encoding='utf-8'?> <!-- The contents of this file ...
- GCD 续集
1.延迟执行 1.1.perform... // 3秒后自动回到当前线程调用 self 的 download: 方法,并且传递参数:@“http://xxx.jpg” [self performSel ...
- C#时间日期操作
一.C# 日期格式 DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.ToFileTime().ToString() ...
- Android存储小结
转自:http://www.liaohuqiu.net/cn/posts/storage-in-android/ android系统自身自带有存储,另外也可以通过sd卡来扩充存储空间.前者好比pc中的 ...
- 如何获取fragment里的控件
不能在onCreate函数中获取控件,以为fragment还没有start,你可以在onStart函数中获取: @Overrideprotected void onStart() { super.on ...