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. 面试大厂,90%会被问到的Java面试题(附答案)

    面向对象的三个特征 封装,继承,多态 多态的好处,代码中如何实现多态,虚拟机中如何实现多态 允许不同类对象对同一消息作出相应,好处如下: 可替换性:多态对已存在的代码具有可替换性 可扩充性:增加新的子 ...

  2. PDF编辑:pdfFactory文本备注功能详解

    除了word的doc文件外,PDF也是我们经常接触到的文件格式,经常需要在pdf文件上进行编辑与修改,或者给内容做提示和备注. 文件的文本备注功能可以用pdfFactory来进行,编辑打印PDF一条龙 ...

  3. js 手机号验证

    1 js 通过正则表达式对手机号进行验证 2 3 var reg = /^1[3|4|5|8][0-9]\d{4,8}$/; 4 var sMobile = document.mobileform.m ...

  4. Spring Boot第一天

    1.首先在idea中创建一个maven项目,创建成功后在pom.xml中添加SpringBoot相关的依赖 <!--引入SpringBoot相关的依赖--> <parent> ...

  5. 第四章:动态规划I

    4.1背包问题 动态规划的核心:如何构造一个高效的备忘录,提高整个问题求解的效率. 4.2最大子数组问题II

  6. [配置]01.IntelliJ IDEA代码格式化与Eclipse保持风格一致

  7. 关于Linux虚拟机连接不上网络的问题

    前阵子自学Linux(版本是CentOS6 -VMware ),因为连不上网的问题搁置了一段时间,昨天又重新拾起来,花了一下午时间终于搞定.下面说几点,给自己学习历程一个记录,也希望能帮到其他初学者. ...

  8. Mockito 结合 Springboot 进行应用测试

    Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring Boot可以跟BDD(Behavier Driven ...

  9. Java基础教程——序列化

    序列化 序列化:Serialize,将Java对象写入IO流(可以保存在数据库,文件等) 反序列化:Deserialize,从IO流中读取并恢复Java对象. 这么理解:序列化就是把对象封印起来,反序 ...

  10. Android多触点总结

    文章部分内容参考: http://blog.csdn.net/barryhappy/article/details/7392326 总结: 1. event.getX()可以得到x的坐标,里面的参数0 ...