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 ...
随机推荐
- 高性能 TCP & UDP 通信框架 HP-Socket v3.2.3 正式宣布
HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包括服务端组件.client组件和 Agent 组件.广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#. ...
- 使用Abator生产ibatis配置文件
什么都不说了,直接进入正题. 插件安装地址:http://ibatis.apache.org/tools/abator 里面有name和url,填了就可以安装了. 通过菜单的 File > Ne ...
- iOS极光推送的基本使用
昨天花了一下午的时间研究了下极光推送,也前也是没做过,不知道从何下手!才开始的时候一看官方的SDK感觉好难,不过经过一系列的捣鼓之后,手机收到了推送信息,感觉其实并没有那么难! 1.配置开发证书(得有 ...
- 跨域文件crossdomain.xml在weblogic上的部署
1.新建一个ROOT文件夹,把crossdomain.xml拷贝到这个文件夹下. 2.在ROOT文件夹下新建一个WEB-INF文件夹 3.在WEB-INF文件夹下建立一个weblogic.xml文件, ...
- gcc -lpthread 干什么用
#include <stdio.h> #include <pthread.h> void *ThreadFunc(void *pArg) //参数的值为123 { int i ...
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- RapidMiner的基本使用(一个医疗数据的简单决策树算法分析)
RapidMiner的基本使用(一个医疗数据的简单决策树算法分析) RapidMiner的基本使用(一个医疗数据的简单决策树算法分析) 需要分析的文件: 右键分别创建读取excel数据,选择属性,设置 ...
- Android 更好的Activity生命周期回调
/** * This class allows you to listen to when the user is entering the background (i.e. after a home ...
- OSC本地库推送到远程库
1.新建远程库: 例如:http://git.oschina.net/intval/learngit 2.本地生成ssh密钥 ssh-keygen -t rsa -C "intval@163 ...
- Silverlight代码编写对控件的PlaneProjection.RotationY属性控制动画
Canvas c; void btnDraw_Click(object sender, RoutedEventArgs e) { Storyboard story = new Storyboard() ...