[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的快速检索能力. ...
随机推荐
- 四则运算_EX
在原有四则运算基础上,除整数以外要支持真分数运算(验证正确性) 一次出的题避免相互重复 可定制出题数目 #include <stdio.h>#include <stdlib.h> ...
- ChipScope Pro Inserter - "ERROR:NgdBuild:924 - bidirect pad net '<oDRAM0_A>' is driving non-buffer primitives
解决方案: Using a IOBUF signal as a trigger for the ILA Inserter flow will cause a NGDBuild error. These ...
- 武汉Uber优步司机奖励政策(8月31日~9月6日)
·奖励前提 *必须满足当周平均评分4.7星及以上,且当周接单率70%及以上,当周在线5小时且完成5单,才有资格获得奖励 * 各组别必须满足当周要求的成单率才有资格获得奖励,成单率由当周 滴滴快车单单2 ...
- Mysql 8.0.* zip版本 windows安装
一,MySQL8.0.*zip版本安装步骤. 1,下载 https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-winx64.zip 注现 ...
- RSA加密通信小结(二)-新版本APP与后台通信交互内容修改方案
注1:本次修改分为两步,首先是内容相关的修改,待其完成之后,再进行加密通信项(粗体字备注)修改. 1.新的提交后台的格式包括:data,token(预留字段,暂时后台不校验),userId(已有的不删 ...
- 不老的神器--namp,awvs
要会使用的工具 NESSUS nmap awvs hydra burpsuit 工具的话,都有文档,应该多使用 -h 多看官方文档,就会用了. 1.namp基本用法 -iL <inputfile ...
- 第六模块:WEB框架开发 第1章·Django框架开发88~128
88-Ajax简介 89-Ajax的简单实现 90-基于Ajax的传递数据 91-基于Ajax的登录验证 92-基于Form表单的文件上传 93-请求头之contentType 94-Ajax传递js ...
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...
- Android开发-API指南-<receiver>
<receiver> 英文原文:http://developer.android.com/guide/topics/manifest/receiver-element.html 采集(更新 ...
- ARM架构中的程序执行与调用
ARM架构中的程序执行与调用 1. 几个名词 ABI : 可执行文件必须遵守的规范,以在特定执行环境中运行: 单独产生的可重定址的文件必须遵守的规范,以用来链接和执行. EABI: 适用于嵌入式环境的 ...