C++ unordered_map
是一种特殊的map,查询键值的复杂度为O(1),但是map查询键值的复杂度为O(log N)
有的编译器使用时要加入下面的头文件:
#if(__cplusplus == 201103L) #include <unordered_map> #include <unordered_set> #else #include <tr1/unordered_map> #include <tr1/unordered_set> namespace std {using std::tr1::unordered_map;using std::tr1::unordered_set; } #endif
下面看几个函数:
#include <bits/stdc++.h> #if(__cplusplus == 201103L) #include <unordered_map> #include <unordered_set> #else #include <tr1/unordered_map> #include <tr1/unordered_set> namespace std {using std::tr1::unordered_map;using std::tr1::unordered_set; } #endif using namespace std; const int maxn = 1e5 + 100; unordered_map<int, int> fa; int main() {fa[1]=2;fa[2]=3;fa[3]=4;fa[4]=0;//count函数 查询键值是否有值 返回0或者1 cout<<fa.count(5)<<endl;//0cout<<fa.count(4)<<endl;//1cout<<fa.count(3)<<endl;//1if(fa.find(1)==fa.end()) cout<<"不存在"<<endl;//存在 else cout<<"存在"<<endl;if(fa.find(4)==fa.end()) cout<<"不存在"<<endl;//存在 else cout<<"存在"<<endl; if(fa.find(5)==fa.end()) cout<<"不存在"<<endl;//不存在 else cout<<"存在"<<endl; //遍历faunordered_map<int,int>::iterator it;for(it=fa.begin();it!=fa.end();){cout<<it->first<<" "<<it->second<<endl;++it;} cout<<endl<<endl;//移除某个键值为奇数的元素 for(it=fa.begin();it!=fa.end();){if(it->first%2==1) it=fa.erase(it); //删除之后指向下一个 else ++it;} for(it=fa.begin();it!=fa.end();){cout<<it->first<<" "<<it->second<<endl;++it;} return 0; }
转载于:https://www.cnblogs.com/caijiaming/p/11482852.html
C++ unordered_map相关推荐
- STL中的map、unordered_map、hash_map
转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...
- C++11 unordered_map详细介绍
整理的算法模板合集: ACM模板 目录: 1.介绍 1.1 特性 2. 模版 2.1 迭代器 3. 功能函数 3.1 构造函数 3.2 容量操作 3.2.1 size 3.2.2 empty 3.3 ...
- C++_STL——unordered_map、unordered_multimap、unordered_set、unordered_multiset
C++_STL--unordered_map.unordered_multimap.unordered_set.unordered_multiset 参考:cplusplus 这几种都是拉链法所写的, ...
- 原 c++中map与unordered_map的区别
c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...
- unordered_map的使用特性
map,.unordered_map, hash_map的比较https://blog.csdn.net/u014209688/article/details/95366594 1.unordered ...
- unordered_map源码解析和个人理解
参考链接 https://blog.csdn.net/ddkxddkx/article/details/6555754 还有这个源码解析https://zrj.me/archives/1248 1.下 ...
- 【C++】unordered_map 使用方法
1 #include <iostream> 2 #include <unordered_map> 3 #include <map> 4 #include <s ...
- C++语言map和unordered_map的下标操作
C++语言map和unordered_map的下标操作 C++语言map和unordered_map的下标操作
- 算法提高课-搜索-双向广搜 AcWing 190. 字串变换:bfs、双向bfs、queue和unordered_map
题目分析 来源:acwing 分析: 双向广搜主要用在最小步数模型(也称状态图模型)里面,这里整个状态空间一般是指数级别的,用双向广搜可以极大地提高运行效率. 双向广搜,顾名思义,就是从起点和终点都进 ...
- linux进程map,linux下unordered_map和map在小数据下性能差异
说起性能,没有测试,还是没有发言权: #include #include #include #include #include #include #include #include #include ...
最新文章
- JavaScript 日期格式化 简单有用
- python编辑器_初学Python这几款编辑器,推荐你安装
- php7.2获取年份的格式,7.7.2 获取zval的值及类型
- #if DEBUG vs. Conditional(“DEBUG”)
- 网络知识 | 《图解HTTP》读书笔记(上)
- c得到当前时分秒 linux_Linuxc - Linux系统下的时间知识点
- mysql数据库服务器名_mysql数据库服务器名
- kafka源码_终于看到有人把Kafka讲清楚了,阿里面试官推荐你看这份源码笔记
- UserData的诡异bug
- f5源站获取http/https访问的真实源IP解决方案
- uniapp动态显示数组_【报Bug】uniapp 在小程序中 动态修改添加 数组 数据 ,点击获取的索引值错误...
- python定义16进制数组,十六进制字符串到python中的字节数组
- 泛微OA ecology 您查看的文档过大,请下载文档后查看
- 花了三天三夜才收集整理出来的经典 SQL 数据库笔试题及答案
- 个人观点:苹果对iPad商标事件的解决办法
- 在计算机上如何打开资源管理器,资源管理器怎么打开?打开资源管理器的5种方法...
- 网络爬虫——豆瓣电影排行榜数据抓取(高级)
- 为什么我的windows 10 电脑关机时,电脑灯延迟很久才熄灭?
- WHQL认证最新申请流程
- Photoshop 系列:照片调色记录
热门文章
- 笔记--Spring in action
- 代码生成利器-NCodeGenerate 教程(2) NCodeGenerate的代码公用之一
- [九度][何海涛] 变态跳台阶
- u-boot工作流程
- 【报告分享】2021日化行业社媒营销报告.pdf(附下载链接)
- 【实践】短视频场景下信息流广告的挑战和技术实践.pdf(附下载链接)
- 你的个人信息真的被偷走了?——那些过分“精准”的广告推荐的背后
- conda加入pytorch清华源
- 2021 年各家大厂的 AI Lab 现状如何?
- [旧文新读] 深度学习在Airbnb搜索的应用实践