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. Bad magic number ImportError in python

    是源码编译里面版本不对,删除掉源码pyc然后重新编译就可以了 find .-name '*.pyc'-delete python -m compileall . 更新历史 why when 创建 20 ...

  2. 设计模式(一)--工厂模式(Go实现)

    package Factory import "fmt" type Restaurant interface { GetFood() } type Donglaishun stru ...

  3. 不要再说不会Spring了!Spring第一天,学会进大厂!

    工作及面试的过程中,作为Java开发,Spring环绕在我们的身边,很多人都是一知半解,本次将用14天时间,针对容器中注解.组件.源码进行解读,AOP概念进行全方面360°无死角介绍,SpringMV ...

  4. FL Studio 插件使用教程 —— 3x Osc(上)

    在FL Studio20 中,3x Osc是继TS404插件之后资历最老的插件之一,也是FL Studio20 中最重要.使用率最高的插件之一.相比别的FL Studio20内置插件,3x Osc 相 ...

  5. ceph 集群快速部署

    1.三台Centos7的主机 [root@ceph-1 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)    2.主机 ...

  6. leetcode 108 和leetcode 109

    //感想:有时候啊,对于一道题目,如果知道那个点在哪,就会非常简单,比如说这两题,将有序的数组转换为二叉搜索树, 有几个点: 1.二叉搜索树:对于某个节点,它的左节点小于它,它的右节点大于它,这是二叉 ...

  7. Java蓝桥杯01——第一题集锦:堆煤球、购物单、哪天返回、第几天、分数

    堆煤球(2016JavaB) 有一堆煤球,堆成三角棱锥形.具体: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形), .... 如果一共有100 ...

  8. CentOS 7定时执行python脚本

    CentOS 7定时执行python脚本 在CentOS下,可以使用crontab进行定时任务的处理. 一.crontab的安装 默认情况下,CentOS 7中已经安装有crontab,如果没有安装, ...

  9. mininet + opendaylight环境配置

    环境配置 ubuntu18.04 镜像 mininet2.2.2 apt-get install mininet 但这种安装只是TLS版本的mininet,与最新版本在功能上有所差距. 控制器(ope ...

  10. python删除list中的空list

    list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText'] 如何删除空列表,以便我得到: list2 = ['text', 'text2 ...