检测map容器是否为空:

 1 #include <iostream>
2 #include<map>
3 #include<string>
4 using namespace std;
5 int main()
6 {
7 //检测容器是否为空
8 map<string, string>mapText;
9 if (mapText.empty())
10 {
11 cout << "mapText为空" << endl;
12 }
13 else
14 {
15 cout << "mapText不为空" << endl;
16 }
17
18 //向容器中添加元素
19 mapText["小A"] = "A"; //赋值
20 mapText["小B"] = "B"; //赋值
21 mapText["小C"] = "C"; //赋值
22
23 if (mapText.empty())
24 {
25 cout << "mapText为空" << endl;
26 }
27 else
28 {
29 cout << "mapText不为空" << endl;
30 }
31
32 system("pause");
33 return 0;
34 }


 重复赋值,值被替换:

 1 #include <iostream>
2 #include<map>
3 #include<string>
4 using namespace std;
5 int main()
6 {
7 map<string, string>mapText;
8 mapText["小A"] = "A"; //赋值
9 mapText["小A"] = "B"; //重复赋值
10 cout << mapText["小A"] << endl;
11
12 system("pause");
13 return 0;
14 }


 判断键是否存在,如果不存在再赋值:

 1 #include <iostream>
2 #include<map>
3 #include<string>
4 using namespace std;
5 int main()
6 {
7 map<string, string>mapText;
8 mapText["小A"] = "A"; //赋值
9 //先检测键是否存在,如果存在则不赋值
10 if (mapText.count("小A") == 0) //count==0不存在 count==1存在
11 {
12 mapText["小A"] = "B"; //重复赋值
13 }
14 cout << mapText["小A"] << endl;
15
16 system("pause");
17 return 0;
18 }


 map循环遍历:

map.begin()指向map的第一个元素

map.end()指向map的最后一个元素之后的地址

 1 #include <iostream>
2 #include<map>
3 #include<string>
4 using namespace std;
5 int main()
6 {
7 map<string, string>mapText;
8 mapText["小A"] = "A"; //赋值
9 mapText["小B"] = "B"; //赋值
10 mapText["小C1"] = "C"; //赋值
11 mapText["小C2"] = "C"; //赋值
12 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor)
13 {
14 cout << "key = " << itor->first << ", value = " << itor->second << endl;
15
16 }
17 system("pause");
18 return 0;
19 }


 map 通过“键”删除键值对:

 1 #include <iostream>
2 #include<map>
3 #include<string>
4 using namespace std;
5 int main()
6 {
7 map<string, string>mapText;
8 mapText["小A"] = "A"; //赋值
9 mapText["小B"] = "B"; //赋值
10 mapText["小C1"] = "C"; //赋值
11 mapText["小C2"] = "C"; //赋值
12 cout << "删除前:" << endl;
13 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor)
14 {
15 cout << "key = " << itor->first << ", value = " << itor->second << endl;
16
17 }
18 //删除小B
19 mapText.erase("小B");
20 cout << "删除后:" << endl;
21 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor)
22 {
23 cout << "key = " << itor->first << ", value = " << itor->second << endl;
24
25 }
26 mapText.erase("小B"); //小B不存在,erase也不会报错
27 system("pause");
28 return 0;
29 }


  map 通过“值”删除键值对,先写一个错误用法,这里要注意:

 1 #include <iostream>
2 #include<map>
3 #include<string>
4 using namespace std;
5 int main()
6 {
7 map<string, string>mapText;
8 mapText["小A"] = "A"; //赋值
9 mapText["小B"] = "B"; //赋值
10 mapText["小C1"] = "C"; //赋值
11 mapText["小C2"] = "C"; //赋值
12 cout << "删除前:" << endl;
13 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor)
14 {
15 cout << "key = " << itor->first << ", value = " << itor->second << endl;
16
17 }
18 //删除值为C的元素
19 //错误用法:
20 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor)
21 {
22 if ((itor->second) == "C")
23 {
24 mapText.erase(itor);
25 }
26 }
27
28 cout << "删除后:" << endl;
29 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor)
30 {
31 cout << "key = " << itor->first << ", value = " << itor->second << endl;
32
33 }
34 system("pause");
35 return 0;
36 }

错误原因:itor指针在元素被删除后失效了,回到for语句中与mapText.end()进行比较出现错误。


 map 通过“值”删除键值对,正确的用法:

 1 #include <iostream>
2 #include<map>
3 #include<string>
4 using namespace std;
5 int main()
6 {
7 map<string, string>mapText;
8 mapText["小A"] = "A"; //赋值
9 mapText["小B"] = "B"; //赋值
10 mapText["小C1"] = "C"; //赋值
11 mapText["小C2"] = "C"; //赋值
12 cout << "删除前:" << endl;
13 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor)
14 {
15 cout << "key = " << itor->first << ", value = " << itor->second << endl;
16
17 }
18 //删除值为C的元素
19 //正确用法:
20 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); /*++itor*/)
21 {
22 if ((itor->second) == "C")
23 {
24 itor = mapText.erase(itor);
25 }
26 else
27 {
28 ++itor;
29 }
30 }
31
32
33 cout << "删除后:" << endl;
34 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor)
35 {
36 cout << "key = " << itor->first << ", value = " << itor->second << endl;
37
38 }
39 system("pause");
40 return 0;
41 }


删除map的第一个元素

mapText.erase(mapText.begin());

【C++】map容器的用法的更多相关文章

  1. 蓝桥杯 算法提高 9-3摩尔斯电码 _c++ Map容器用法

    //****|*|*-**|*-**|--- #include <iostream> #include <map> #include <vector> #inclu ...

  2. 详解C++ STL map 容器

    详解C++ STL map 容器 本篇随笔简单讲解一下\(C++STL\)中的\(map\)容器的使用方法和使用技巧. map容器的概念 \(map\)的英语释义是"地图",但\( ...

  3. map的详细用法

     map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  4. stl之map容器的原理及应用

    容器的数据结构同样是采用红黑树进行管理,插入的元素健位不允许重复,所使用的节点元素的比较函数,只对元素的健值进行比较,元素的各项数据可通过健值检索出来.map容器是一种关联容器,实现了SortedAs ...

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

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

  6. map的详细用法 (转

    map的详细用法: map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能 力,由于这个特性,它完成有可能在我 ...

  7. C++ STL 中 map 容器

    C++ STL 中 map 容器 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它 ...

  8. map的常见用法

    map的常见用法 map 是什么? map是一组键值对的组合,通俗理解类似一种特殊的数组,a[key]=val,只不过数组元素的下标是任意一种类型,而且数组的元素的值也是任意一种类型.有点类似pyth ...

  9. map 容器的使用

    C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明    1   头文件   #include   <map> ...

随机推荐

  1. 记一次 .NET 医院CIS系统 内存溢出分析

    一:背景 1. 讲故事 前几天有位朋友加wx求助说他的程序最近总是出现内存溢出,很崩溃,如下图: 和这位朋友聊下来,发现他也是搞医疗的,哈哈,.NET 在医疗方面还是很有市场的,不过对于内存方面出的问 ...

  2. 【目录】Java项目开发中的知识记录

    此篇文章为学习Java的目录,<a href="#"></>这种的是还没有写的文章.已经加a标签的是已经写完的.没写的文章急切需要的话可以直接留言,不是特别 ...

  3. LeetCode 26. 删除有序数组中的重复项

    双指针法 分析: 设置两个指针:p1,p2,初始p1指向数组的第一个元素,p2指向第二个元素 1)如果p1的值 == p2的值,就让p2后移一位 2)如果p1的值 != p2的值,修改p1的下一个元素 ...

  4. 改善c++程序的150个建议(读后总结)-------12-18

    12.优先使用前置操作符 #include <iostream> using namespace std; class A { private: int num; public: A op ...

  5. Summer——从头开始写一个简易的Spring框架

    Summer--从头开始写一个简易的Spring框架                ​ 参考Spring框架实现一个简易类似的Java框架.计划陆续实现IOC.AOP.以及数据访问模块和事务控制模块. ...

  6. OCR横向评测 -- 软工案例分析

    目录 第一部分 调研&评测 使用感受 1. 使用门槛 2. 界面设计 3. 数据标注 4. 模型训练 5. 模型预测 6. 体验评价与改进建议 好的方面: 可能需要改进的方面: 7. BUG反 ...

  7. Mybatis-Plus的应用场景及注入SQL原理分析

    一.背景 1.1 传统Mybatis的弊端 1.1.1 场景描述 假设有两张表:一张商品表.一张订单表,具体表的字段如下: 现有如下需求: 分别根据id查询商品表和订单表所有信息 根据支付状态和通知状 ...

  8. java基础——简易计算器的实现

    计算器: import java.util.Scanner;​public class CalculateDemo {    public static void main(String[] args ...

  9. [Qt] 事件机制(二)

    在samp4_1中加一个小功能,点击右上角关闭按钮时,弹出"确认是否关闭"的消息框.如果点"yes"则关闭,如果点"No"则不关闭 在wid ...

  10. 怎么用优启通安装win7 !!!!好好好20191020

    怎么用优启通安装win7 PE技术探索在国内属于前沿梯队.相关PE工具更新的非常及时,两个月一更新,很赞. 尤其是论坛代表作之一:EasyImageX系统备份恢复镜像工具(集成在PE里面),可以说是用 ...