【C++】map容器的用法
2024-08-28 22:24:00
检测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());
最新文章
- git将本地仓库推送到远程仓库
- Angular 2 要来了,Wijmo 已准备好迎接
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
- 单例(C#版)
- QT中16进制字符串转汉字
- 安卓反汇编工具arm-eabi-objdump
- 菜鸟学Java(二十一)——怎样更好的进行单元測试——JUnit
- .Net Framework基础知识
- CSS居中方法搜集
- 366. Find Leaves of Binary Tree C#
- 实现微信浏览器自动播放MP3音乐
- Python中的单例模式
- Session、Cookie 学习笔记
- 基于MT6752/32平台 Android L版本驱动移植步骤
- html_学习地址
- $Django 在线文本编辑器skindeditor
- phpBB3导入帖子的Python脚本
- leetcode337
- windows 远程连接“发生身份验证错误 要求的函数不受支持”
- 【Java算法】输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
热门文章
- Day008 数组的使用
- ColyseusJS 轻量级多人游戏服务器开发框架 - 中文手册(系统保障篇)
- 解决移动端300ms延迟fastclick
- Pytorch系列:(五)CNN
- Python 送你一棵圣诞树
- sed -i &#39;s/Search_String/Replacement_String/g&#39; Input_File sed详细手册
- 什么是环境变量,Linux环境变量及作用 echo
- centos7下cups + samba共打印服务
- windows怎么访问linux的samba共享目录
- 056.Python前端Django模型ORM多表基本操作