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基本用法的更多相关文章

  1. C++中map和unordered_map的用法

    1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...

  2. 2.9 C++STL map/multimap容器详解

    文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...

  3. 关于c++ STL map 和 unordered_map 的效率的对比测试

    本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...

  4. STL::map/multimap

    map: 默认根据 key 排序(从小到大),能够通过 backet operator(operator [ ]) 来获取元素,内部由二叉搜索树来实现(binary search trees). mu ...

  5. C++使用: C++中map的基本操作和用法

    在阅读SSD代码中发现作者使用了C++中的map方法,因此搜索该关联式容器的使用方法,在这里一并总结. 一.Map 簡介 Map是STL的一個容器,它提供一對一的hash. 第一個可以稱為關鍵字(ke ...

  6. STL——map/unordered_map基础用法

    map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key,  ...

  7. STL:map/multimap用法详解

    map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...

  8. STL之六:map/multimap用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap 使用map/multimap之前要加入头文件# ...

  9. (转载)STL map与Boost unordered_map的比较

    原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...

  10. STL Map和multimap 容器

    STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力.       ...

随机推荐

  1. 关于具有I2C总线的TEA6320的使用

    现在先了解一下TEA6320,TEA6320是一个I2C总线控制音响应用的立体声放大器,,它的I2C协议和音量控制如下: 它的主要代码: void delay1ms(unsigned int Dela ...

  2. C# base64 转 byte[]

    string转成 Base64 形式的String //byte[] 转string byte[] b = Encoding.Default.GetBytes("字符串"); // ...

  3. 成都Uber优步司机奖励政策(2月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. VINS(六)边缘化

    通常的边缘化是将联合概率分布分解为边缘概率分布和条件概率分布的过程,这样可以将Sliding Window中较旧的状态边缘化出Sliding Window,同时保留其信息.并且保证了对应H海塞矩阵的稀 ...

  5. SpringBoot学习:整合MyBatis,使用Druid连接池

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)添加pom依赖: <!-- https://mvnrepository.co ...

  6. Qml-Dialog不能隐藏标题栏和按钮自定义

    在项目中,需要弹出一个对话框来完成用户输入的功能,为了考虑界面的同一,这里需要将原生自带的标题栏隐藏掉,换成自己写的 按照widget的写法,可以使用QDialog,但是qml与之对应的Dialog我 ...

  7. jmeter "you cannot switch bacause data cannot be converted to target Tab data,empty data to switch"报错

    jmeter "you cannot switch bacause data cannot be converted to target Tab data,empty data to swi ...

  8. selenium自动化测试资源整理

    1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是原版的就不得而知了. http://www.slimjet.com/ch ...

  9. Appium_Python_API说明

    Appium_Python_API 1.contexts contexts(self): Returns the contexts within the current session. 返回当前会话 ...

  10. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第2章.HTTP协议简介

    第2章--HTTP协议简介 HTTP协议简介 Abstract: HTTP协议的特性,HTTP请求/响应的过程,HTTP请求/响应的报文格式等知识,最后会演示如何通过Chrome提供的开发者工具,去跟 ...