【C++】map容器的用法

摘要:
=地图文本。end();++itor)22{23cout˂˂“key=”˂第一个˂˂“,value=”˂第二个˂˂endl;2425}26map文本。擦除;//小B不存在,擦除不会报告27系统的错误;28返回0;29}映射通过“value”删除键值对,并首先写入错误用法。这里注意:1#include<iostream>2#include<map>3#include<string>4usingspacestd;5intmain()6{7map˂string,string>mapText;8mapText[“Small A”]=“A”;//分配9mapText[”Small B“]=“B”;//指定10mapText[(Small C1”]=”C“;//分配11mapText[”Small C2“]=”C“;//指定12cout<“删除前:”<endl;13 for(map:iterator=mapText.begin();itor!

检测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 }

 【C++】map容器的用法第1张


 重复赋值,值被替换:

 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 }

【C++】map容器的用法第2张


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

 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 }

【C++】map容器的用法第3张


 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 }

【C++】map容器的用法第4张


 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 }

【C++】map容器的用法第5张


  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 }

【C++】map容器的用法第6张


删除map的第一个元素

mapText.erase(mapText.begin());

免责声明:文章转载自《【C++】map容器的用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇小白自学SQLServer----1. 安装数据库 SQL Server 2014(安装到最后,不小心点击取消怎么办T__T)教你调用数据库读取短信 记事本 通讯录文件,让ios5的短信恢复到ios4下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#发送邮件(使用SSL,587端口)

static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"]; static readonly string userName = System.Configuration.Configurati...

在springboot启动时给钉钉群发通知

1.因为springboot启动后会加载所用的配置文件,所以我们可以在main方法下写DingTalk的bean来注入DingTalk配置。 @ServletComponentScan public classApplication { //DingTalk Bean变量 private static String DING_TALK_U...

YARN应用程序的开发步骤

开发基于YARN的应用程序需要开发客户端程序和AppMaster程序: 我们基于程序自带的例子来实现提交application 到YARN的ResourceManger。 Distributed Shell application:Client 步骤: 连接  ResourceManager; 通过ApplicationClientProtocol协议 与...

全面解决.Net与Java互通时的RSA加解密问题,使用PEM格式的密钥文件

一、缘由 RSA是一种常用的非对称加密算法。所以有时需要在不用编程语言中分别使用RSA的加密、解密。例如用Java做后台服务端,用C#开发桌面的客户端软件时。由于 .Net、Java 的RSA类库存在很多细节区别,尤其是它们支持的密钥格式不同。导致容易出现“我加密的数据对方不能解密,对方加密的数据我不能解密,但是自身是可以正常加密解密”等情况。虽然网上已经...

Android笔记之强大的buildConfigField

在进行项目开发或维护时,经常会遇到调试和发布所用到的参数值不一致的情况 例如,服务器会分测试和正式,为了能方便地更改(自动更换)服务器地址,buildConfigField就派上用场了 以前都是手动更改的,极易出错T_T buildConfigField语法如下 buildConfigField "TypeName", "FieldName", "Fiel...

EhCache之最简单的缓存框架

一、简介 Ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案。同时ehcache作为开放源代码项目,采用限制比较宽松的Apache License V2.0作为授权方式,被广泛地用于Hibernate, Spring,Cocoon等其他...