Map & multimap 的删除

  • map.clear();           //删除所有元素
  • map.erase(pos);      //删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • map.erase(beg,end);//删除区间[beg,end)的所有元素  ,返回下一个元素的迭代器。
  • map.erase(key);     //删除容器中key为key的对组,返回删除的对组个数

1. clear() 删除所有元素示例:

 1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu1;
9 mapStu1.insert(pair<int, string>(1, "内容A"));
10 mapStu1.insert(pair<int, string>(2, "内容B"));
11 mapStu1.insert(pair<int, string>(3, "内容C"));
12
13 if (!mapStu1.empty())
14 {
15 cout << "容器 mapStu1 中共有" << mapStu1.size() << "个元素" << endl;
16 }
17 else
18 {
19 cout << "容器 mapStu1 中共有" << mapStu1.size() << "个元素, " << "容器为空" << endl;
20 }
21
22 cout << "删除所有元素" << endl;
23 mapStu1.clear();
24 if (!mapStu1.empty())
25 {
26 cout << "容器 mapStu1 中共有" << mapStu1.size() << "个元素" << endl;
27 }
28 else
29 {
30 cout << "容器 mapStu1 中共有" << mapStu1.size() << "个元素, " << "容器为空" << endl;
31 }
32
33 return 0;
34 }

打印结果:

2. erase(pos);  //删除pos迭代器所指的元素示例:

 1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu1;
9
10 mapStu1.insert(pair<int, string>(1, "内容A"));
11 mapStu1.insert(pair<int, string>(2, "内容B"));
12 mapStu1.insert(pair<int, string>(3, "内容C"));
13 mapStu1.insert(pair<int, string>(4, "内容D"));
14
15 map<int, string>::iterator it1 = mapStu1.begin();
16 cout << "删除首元素前, 首元素为:" << it1->first << "首元素内容为: " << it1->second << endl;
17 cout << endl;
18 map<int, string>::iterator it2 = mapStu1.erase(mapStu1.begin());
19 cout << "删除首元素后, 新的首元素为:" << it2->first << "首元素内容为: " << it2->second << endl;
20
21 return 0;
22 }

打印结果:

3. erase(beg,end); //删除区间[beg,end)的所有元素示例

 1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu1;
9
10 mapStu1.insert(pair<int, string>(1, "内容A"));
11 mapStu1.insert(pair<int, string>(2, "内容B"));
12 mapStu1.insert(pair<int, string>(3, "内容C"));
13 mapStu1.insert(pair<int, string>(4, "内容D"));
14 mapStu1.insert(pair<int, string>(5, "内容E"));
15
16 cout << "遍历mapStu1: " << endl;
17 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++)
18 {
19 cout << "容器 mapStu1 的第" << it->first << "个元素为: " << it->second << endl;
20 }
21
22 cout << endl << "删除除首元素与尾部元素之间的所有元素" << endl;
23
24 map<int, string>::iterator it = mapStu1.erase(++mapStu1.begin(), --mapStu1.end());
25 cout << endl << "删除完后返回的迭代器所指向的 key 为: " << it->first << "内容为: " << it->second << endl;
26
27 cout << endl << "删除后遍历mapStu1: " << endl;
28 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++)
29 {
30 cout << "容器 mapStu1 的第" << it->first << "个元素为: " << it->second << endl;
31 }
32
33 return 0;
34 }

打印结果:

4. erase(key); //删除容器中key为key的对组示例

 1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 multimap<int, string> multimapStu1;
9
10 multimapStu1.insert(pair<int, string>(1, "内容A"));
11 multimapStu1.insert(pair<int, string>(1, "内容AA"));
12 multimapStu1.insert(pair<int, string>(1, "内容AAA"));
13 multimapStu1.insert(pair<int, string>(2, "内容B"));
14 multimapStu1.insert(pair<int, string>(3, "内容C"));
15
16 cout << "遍历mapStu1: " << endl;
17 for (map<int, string>::iterator it = multimapStu1.begin(); it != multimapStu1.end(); it++)
18 {
19 cout << "容器 mapStu1 key 为" << it->first << "的个元素为: " << it->second << endl;
20 }
21 cout << endl << "删除所有 key 为 1 的组" << endl;
22
23 cout << endl << "共删除了" << multimapStu1.erase(1) << "个 key 为1的组" << endl; //会返回删除的个数
24 cout << "遍历mapStu1: " << endl;
25 for (map<int, string>::iterator it = multimapStu1.begin(); it != multimapStu1.end(); it++)
26 {
27 cout << "容器 mapStu1 key 为" << it->first << "的个元素为: " << it->second << endl;
28 }
29
30 return 0;
31 }

打印结果:

=====================================================================================================================

STL——容器(Map & multimap)的删除的更多相关文章

  1. STL:map/multimap用法详解

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

  2. STL之map&multimap使用简介

    map 1.insert 第一种:用insert函数插入pair数据 #include <map> #include <string> #include <iostrea ...

  3. STL容器 -- Map

    核心描述: map 就是从键(key) 到 值(value) 的一个映射.且键值不可重复,内部按照键值排序. 头文件: #include <map> 拓展: multimap 是一个多重映 ...

  4. STL容器Map

    Map的常见函数 Map的实现机制 STL中的Map底层实现机制是RB树(红-黑树)

  5. 【STL】-Map/Multimap的用法

    初始化: map<string,double> salaries; 算法: 1. 赋值.salaries[ "Pat" ] = 75000.00; 2. 无效的索引将自 ...

  6. STL - 容器 - Map(二)

    把Map用作关联式数组 MapAdvanceTest.cpp #include <map> #include <string> #include <iostream> ...

  7. STL - 容器 - Map(一)

    MapTest.cpp #include <map> #include <string> #include <iostream> #include <algo ...

  8. iBinary C++STL模板库关联容器之map/multimap

    目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...

  9. STL 笔记(二) 关联容器 map、set、multimap 和 multimap

    STL 关联容器简单介绍 关联容器即 key-value 键值对容器,依靠 key 来存储和读取元素. 在 STL 中,有四种关联容器,各自是: map 键值对 key-value 存储,key 不可 ...

随机推荐

  1. DevOps,你真的了解吗?

    与大数据和PRISM(NSA的监控项目之一),DevOps(开发运维)如今是科技人士挂在嘴边的热词,但遗憾的是,类似圣经,每个人都引用DevOps的只言片语,但真正理解并能执行的人极少.根据CA的一项 ...

  2. 有关线上系统点击没有任何相应得问题思考,主要针对PC端应用程序

    1.问题得起因 前段时间,客户得某些机器上,点击应用系统得快捷方式,没有任何响应,不弹出程序主界面,也没有任何得报错提示,甚至程序得错误日志也没有任何输出. 当时,听说发生这种情况得时候,有点懵了,不 ...

  3. python的pip快速安装代码

    pip install xx,经常由于网速,或者安装版本问题导致安装速度慢超时等问题, 现提供一个py镜像安装代码,安装库文件前执行下这个程序,可以很快下载 cmd 进入命令提示符 python .p ...

  4. 创建Spring Cloud聚合项目

    使用maven创建单一项目的时候通常用不到聚合项目,创建spring cloud项目时候,由于下面都是一个一个微服务,每个服务对应一个项目,这就需要用到聚合项目,方便对依赖和项目之间的关系进行管理,使 ...

  5. linux中的fork炸弹

    学习bash脚本看到一段代码(老鸟应该知道)挺有意思,一时看不懂.该命令不需要管理员即可运行,请不要在你的机器上使用以下脚本,否则你知道你在干什么 :() { :|: & };: 参考链接:h ...

  6. TCP的ACK机制

    下面是整个的tcp的三次握手和四次挥手的协议 TCP四次挥手 在客户端先发送一个FIN的包,表示要close(),客户端想和连接断开,发完之后出于FIN_WAIT_1状态下:服务端收到之后就变成CLO ...

  7. zabbix的搭建及操作(3)监控 MySQL 及 HTTP 状态监控

    书接上回 -- 详情点击 Server端以配置好 mariadb(MySQL) 及 http 服务 Zabbix实现监控 mysql 数据库 server服务器端配置 vim /usr/local/z ...

  8. guitar pro系列教程(二十七):Guitar Pro教程之理解记谱法

    前面的章节我们讲解了很多关于Guitar Pro'的功能使用,今天小编还是采用图文结合的方式为大家讲解它的理解记谱法,对于很多新人来说,在我们看谱之前,我们肯定要先熟悉他的一些功能如何使用以及一些关于 ...

  9. C语言讲义——内联函数

    如果一些函数被频繁调用,不断地有函数入栈(Stack),会造成栈空间的大量消耗. 对应这种问题,可以使用内联函数(inline). 编译器会将内联函数的代码整段插入到调用的位置. #include & ...

  10. Goland 2020.2.x 激活码永久破解教程 (最新Goland激活码!2020.11.26亲测可用!)

    在2020.11.26 Goland的用户们又迎来了一次更新,这就导致很多软件打开时候就提示Goland激活码已经失效,码小辫第一时间给各位分享了关于最新Goland激活破解教程! goland已经更 ...