C++中map和unordered_map的用法
1. 简介
map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的。
2. map
对于map的底层原理,是通过红黑树(一种非严格意义上的平衡二叉树)来实现的,因此map内部所有的数据都是有序的,map的查询、插入、删除操作的时间复杂度都是O(logn)。此外,map的key需要定义operator <,对于一般的数据类型已被系统实现,若是用户自定义的数据类型,则要重新定义该操作符。
map的基本操作如下
#include<iostream>
#include<map>
#include<string> using namespace std; int main()
{
// 构造函数
map<string, int> dict; // 插入数据的三种方式
dict.insert(pair<string,int>("apple",2));
dict.insert(map<string, int>::value_type("orange",3));
dict["banana"] = 6; // 判断是否有元素
if(dict.empty())
cout<<"该字典无元素"<<endl;
else
cout<<"该字典共有"<<dict.size()<<"个元素"<<endl; // 遍历
map<string, int>::iterator iter;
for(iter=dict.begin();iter!=dict.end();iter++)
cout<<iter->first<<ends<<iter->second<<endl; // 查找
if((iter=dict.find("banana"))!=dict.end()) // 返回一个迭代器指向键值为key的元素,如果没找到就返回end()
cout<<"已找到banana,其value为"<<iter->second<<"."<<endl;
else
cout<<"未找到banana."<<endl; if(dict.count("watermelon")==0) // 返回键值等于key的元素的个数
cout<<"watermelon不存在"<<endl;
else
cout<<"watermelon存在"<<endl; pair<map<string, int>::iterator, map<string, int>::iterator> ret;
ret = dict.equal_range("banana"); // 查找键值等于 key 的元素区间为[start,end),指示范围的两个迭代器以 pair 返回
cout<<ret.first->first<<ends<<ret.first->second<<endl;
cout<<ret.second->first<<ends<<ret.second->second<<endl; iter = dict.lower_bound("boluo"); // 返回一个迭代器,指向键值>=key的第一个元素。
cout<<iter->first<<endl;
iter = dict.upper_bound("boluo"); // 返回一个迭代器,指向值键值>key的第一个元素。
cout<<iter->first<<endl;
return 0;
}
注意如果定义了map<string,int>这个类型,需要在头文件中包含“#include<string>”,这是因为默认的string是系统的xstring对象,但是没有定义operator<,从而报错。map用到自定义的类型,一定要定义operator<,具体用法如下。
struct person
{
string name;
int age; person(string name, int age)
{
this->name = name;
this->age = age;
} bool operator < (const person& p) const
{
return this->age < p.age;
}
}; map<person,int> m;
3. unordered_map
unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value。不同的是unordered_map不会根据key的大小进行排序,存储时是根据key的hash值判断元素是否相同,即unordered_map内部元素是无序的。unordered_map的底层是一个防冗余的哈希表(开链法避免地址冲突)。unordered_map的key需要定义hash_value函数并且重载operator ==。
哈希表最大的优点,就是把数据的存储和查找消耗的时间大大降低,时间复杂度为O(1);而代价仅仅是消耗比较多的内存。哈希表的查询时间虽然是O(1),但是并不是unordered_map查询时间一定比map短,因为实际情况中还要考虑到数据量,而且unordered_map的hash函数的构造速度也没那么快,所以不能一概而论,应该具体情况具体分析。
unordered_map的基本操作
#include<string>
#include<iostream>
#include<unordered_map>
using namespace std; int main()
{
unordered_map<string, int> dict; // 声明unordered_map对象 // 插入数据的三种方式
dict.insert(pair<string,int>("apple",2));
dict.insert(unordered_map<string, int>::value_type("orange",3));
dict["banana"] = 6; // 判断是否有元素
if(dict.empty())
cout<<"该字典无元素"<<endl;
else
cout<<"该字典共有"<<dict.size()<<"个元素"<<endl; // 遍历
unordered_map<string, int>::iterator iter;
for(iter=dict.begin();iter!=dict.end();iter++)
cout<<iter->first<<ends<<iter->second<<endl; // 查找
if(dict.count("boluo")==0)
cout<<"can't find boluo!"<<endl;
else
cout<<"find boluo!"<<endl; if((iter=dict.find("banana"))!=dict.end())
cout<<"banana="<<iter->second<<endl;
else
cout<<"can't find boluo!"<<endl; return 0;
}
nordered_map用到自定义的类型,需要对key定义hash_value函数并且重载operator ==,具体用法请参考文献。
C++中map和unordered_map的用法的更多相关文章
- 原 c++中map与unordered_map的区别
c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...
- C++使用: C++中map的基本操作和用法
在阅读SSD代码中发现作者使用了C++中的map方法,因此搜索该关联式容器的使用方法,在这里一并总结. 一.Map 簡介 Map是STL的一個容器,它提供一對一的hash. 第一個可以稱為關鍵字(ke ...
- 浅谈c++中map插入数据的用法
map:数据的插入 在构造map容器后,我们就可以往里面插入数据了.这里讲三种插入数据的方法:第一种:用insert函数插入pair数据 map<int, string> mapStude ...
- python中map()和dict()的用法
map()用法 map()是python的内置函数,会根据提供的函数对指定序列做映射. 语法: map(func, iter, ...) 其中func为一个功能函数,iter表示可迭代参数序列.map ...
- 廖雪峰教程笔记:js中map和reduce的用法
举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个数组[1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map实现如下: 由于map()方法定义在JavaScript的 ...
- STL中map,set的基本用法示例
本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...
- python中map()函数用法
map函数的原型是map(function, iterable, …),它的返回结果是一个列表. 参数function传的是一个函数名,可以是python内置的,也可以是自定义的. 参数iterabl ...
- c++ 中map 的find 函数用法
Map中,find函数用来定位数据出现位置,当含有该数据,即查找成功时,返回数据所在未知的迭代器, 如果查找失败,则返回end()函数所在的迭代器,因此用是否等于end来判断是否查找成功. 程序示例: ...
- map和unordered_map的差别和使用
map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/billcyj/article/details/7 ...
随机推荐
- asp.net上传超大文件解决方案
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
- BZOJ 3667: Rabin-Miller算法 (Pollard-Rho 模板)
说实话,我知道每一步都干啥,但我完全不知道为啥这么做,也不知道为什么是正确的,反正会用就行了~ #include <cmath> #include <cstdio> #incl ...
- vfprintf()函数
函数声明:int vfprintf(FILE *stream, const char *format, va_list arg) 函数参数: stream—这是指向了FILE对象的指针,该FILE对象 ...
- CentOS7 安装JumpServer
环境: CentOS Linux release 7.6.1810 (Core) JumpServer 1.4.8 Python 3.6.X MariaDB 编译安装Python3.6 首先,下载Py ...
- Vue_(组件)实例方法
Vue.js实例方法/生命周期 传送门 常用的实例方法 数据: 传送门 vm.$set:设置属性值 vm.$delete:删除属性值 vm.$watch:观测数据变化 生命周期 vm.$mount:手 ...
- 如何卸载zabbix且删除
1.彻底卸载zabbix和删除残留文件 1 2 [root@localhost etc]# service zabbix stop //这个命令是停止服务 [root@localhost et ...
- JIRA备份,数据迁移以及小问题
Jira的备份(切记将许可证号备份) Jira默认会打开自动备份的功能,备份路径为/var/atlassian/application-data/jira/export 管理员账号登录Jira,点击右 ...
- [CSP-S模拟测试]:D(暴力+剪枝)
题目传送门(内部题47) 输入格式 第一行一个正整数$n$.第二行$n$个正整数,表示序列$A_i$. 输出格式 一行一个正整数,表示答案. 样例 样例输入: 530 60 20 20 20 样例输出 ...
- 20165213 Exp6 信息搜集与漏洞扫描
信息搜集与漏洞扫描 一. 实践内容 (1)各种搜索技巧的应用 利用Google Hacking Datebase搜索. 尝试搜索http相关的漏洞,可以看到漏洞的相关信息. 也可以使用过滤器进行过滤, ...
- laravel 中first和find区别(总结一)
检索单个模型/集合 除了从指定的数据表检索所有记录外,你也可以通过 find 或 first 方法来检索单条记录.这些方法不是返回一组模型,而是返回一个模型实例: // 通过主键取回一个模型... $ ...