[STL] map,multimap,unordered_map基本用法
map的特性是,所有元素都会根据元素的键值自动被排序。map的所有元素都是pair,同时拥有键值(key)和实值(value)。pair的第一元素被视为键值,第二元素被视为实值。map不允许两个元素拥有相同的键值
multimap的特性以及用法与map完全相同,唯一的差别在于它允许键值重复。unordered_map与map的区别就在于不会根据key的大小进行排序.
1.插入数据的方法
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> Map1,Map2;
//insert函数插入数据
Map1.insert(pair<int, string>(, "A"));
Map1.insert(pair<int, string>(, "B"));
Map1.insert(pair<int, string>(, "C"));
map<int, string>::iterator iter;
cout<<"Map1:"<<endl;
for(iter = Map1.begin(); iter != Map1.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<" ";
} //数组方式插入数据
Map2[] = "A";
Map2[] = "B";
Map2[] = "C";
cout<<endl<<"Map2:"<<endl;
for(iter = Map2.begin(); iter != Map2.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<" ";
}
return ;
}

使用insert函数和使用数组插入数据的区别就在于:
用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作
是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值。
2.数据的查找(包括判定这个关键字是否在map中出现)
(1)若要实现判断一个key是否存在,如果存在就输出,不存在就不输出的功能,则可以使用count函数。count函数的功能是统计关键字出现的次数。map对于关键字来说是唯一的,也就是说在map中不存在等价的两个(以上)元素,因此某个元素在map/set中出现的次数最多只能为1,用count得到的结果不是0就是1。
(2)用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> Map;
//insert函数插入数据
Map.insert(pair<int, string>(, "A"));
Map.insert(pair<int, string>(, "B"));
Map.insert(pair<int, string>(, "C"));
map<int, string>::iterator iter;
//返回key值为1的迭代器位置
iter = Map.find();
if(iter != Map.end())
cout<<"Find,the value is "<<iter->second<<endl;
else
cout<<"Not find"<<endl;
return ;
}

3.数据的清空与判空
清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map
4. 数据的删除
这里要用到erase函数,它有三个重载了的函数,下面在例子中详细说明它们的用法
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> Map;
//insert函数插入数据
Map.insert(pair<int, string>(, "A"));
Map.insert(pair<int, string>(, "B"));
Map.insert(pair<int, string>(, "C"));
map<int, string>::iterator iter; //用迭代器删除key为2的数据
iter = Map.find();
Map.erase(iter);
//打印输出
for(iter = Map.begin(); iter != Map.end(); iter ++)
{
cout<<iter->first<<" "<<iter->second<<" ";
}
cout<<endl; Map.insert(pair<int, string>(, "B")); //用关键字删除key为2的数据
int n = Map.erase();//删除成功返回1,否则返回0
if(n == )
{
//打印输出
for(iter = Map.begin(); iter != Map.end(); iter ++)
{
cout<<iter->first<<" "<<iter->second<<" ";
}
}
cout<<endl; Map.insert(pair<int, string>(, "B")); //用迭代器,成片的删除
//把整个map清空
Map.erase(Map.begin(),Map.end());
for(iter = Map.begin(); iter != Map.end(); iter ++)
{
cout<<iter->first<<" "<<iter->second<<" ";
}
return ;
}

[STL] map,multimap,unordered_map基本用法的更多相关文章
- C++中map和unordered_map的用法
1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...
- 2.9 C++STL map/multimap容器详解
文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...
- 关于c++ STL map 和 unordered_map 的效率的对比测试
本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...
- STL::map/multimap
map: 默认根据 key 排序(从小到大),能够通过 backet operator(operator [ ]) 来获取元素,内部由二叉搜索树来实现(binary search trees). mu ...
- C++使用: C++中map的基本操作和用法
在阅读SSD代码中发现作者使用了C++中的map方法,因此搜索该关联式容器的使用方法,在这里一并总结. 一.Map 簡介 Map是STL的一個容器,它提供一對一的hash. 第一個可以稱為關鍵字(ke ...
- STL——map/unordered_map基础用法
map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key, ...
- STL:map/multimap用法详解
map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...
- STL之六:map/multimap用法详解
转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap 使用map/multimap之前要加入头文件# ...
- (转载)STL map与Boost unordered_map的比较
原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...
- STL Map和multimap 容器
STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力. ...
随机推荐
- PTA基础编程题目集7-2然后是几点
有时候人们用四位数字表示一个时间,比如1106表示11点零6分.现在,你的程序要根据起始时间和流逝的时间计算出终止时间. 读入两个数字,第一个数字以这样的四位数字表示当前时间,第二个数字表示分钟数,计 ...
- js input 不可编辑可传值设置
在表单提交中,设置input不可编辑,但是可以向后台传输数据,的设置方法: $('#input').attr("readonly",true);
- 北京Uber优步司机奖励政策(11月9日~11月15日)
用户组:人民优步“关羽组”(适用于11月9日-11月15日)奖励政策: 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月 ...
- python的bif介绍
Python是面向对象的解释性程序设计语言,Python的语法简洁,特点是用空白符作为语句缩进. BIF(bulit in function)内置函数,就是Python自身提供的函数功能,编程者直接使 ...
- Odd CSS syntax. [class^='icon-'], [class*=' icon-']
原文: https://stackoverflow.com/questions/20322740/odd-css-syntax-class-icon-class-icon I am going thr ...
- React中类定义组件constructor 和super
刚开始学习React没多久,在老师的教程里看到了类组件的使用示例,但是和资料上有些冲突,而引发了一些疑问: 类组件中到底要不要定义构造函数constructor()? super()里边到底要不要传入 ...
- python里pickle模块
Pickle模块用于将复杂的文件转化为二进制的文件 pickle模块一般是在源代码里面含有较大的字典或者列表等复杂文件时,我们如果将文件直接写在源代码里面,这样会使得代码很冗余,并且源代码文件所占空间 ...
- jupyter notebook 使用cmd命令窗口打开
第一步:将文件路径改为你需要使用文件所在的路径 第二部: jupyter notebook
- opencv-学习笔记(2)
opencv-学习笔记(2) 这章记录了 获取像素点,改变像素点 获取图像的属性(行,列,通道数,数据类型) roi感应区 拆分以及合并图像通道 边缘扩充 opencv获取像素点,改变像素点 ---- ...
- Spring Boot - Filter实现简单的Http Basic认证
Copy自http://blog.csdn.net/sun_t89/article/details/51916834 @SpringBootApplicationpublic class Spring ...