- #include
- #include
- #include
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<
first<<" "<second< - }
- }
第二种:用insert函数插入value_type数据
- #include
- #include
- #include
- using namespace std;
- Int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(map<int, string>::value_type (1, "student_one"));
- mapStudent.insert(map<int, string>::value_type (2, "student_two"));
- mapStudent.insert(map<int, string>::value_type (3, "student_three"));
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<
first<<" "<second< - }
- }
第三种:用数组方式插入数据
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent[1] = "student_one";
- mapStudent[2] = "student_two";
- mapStudent[3] = "student_three";
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<
first<<" "<second< - }
- }
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,
但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明:
- mapStudent.insert(map<int, string>::value_type (1, "student_one"));
-
- mapStudent.insert(map<int, string>::value_type (1, "student_two"));
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
- pair
-
- Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。 下面给出完成代码,演示插入成功与否问题:
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- pair
- Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));
- if(Insert_Pair.second == true)
- {
- cout<<"Insert Successfully"<
- }
- else
- {
- cout<<"Insert Failure"<
- }
- Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
- if(Insert_Pair.second == true)
- {
- cout<<"Insert Successfully"<
- }
- else
- {
- cout<<"Insert Failure"<
- }
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<
first<<" "<second< - }
- }
大家可以用如下程序,看下用数组插入在数据覆盖上的效果
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent[1] = "student_one";
- mapStudent[1] = "student_two";
- mapStudent[2] = "student_three";
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
-
-
相关阅读:
这几个Python实战项目,让大家了解到它的神奇
高级进阶班1——补充(利用平凡解优化流程、通过记录结构找到可能性划分的边界情况、动态规划填表的斜率优化技巧、上中位数结构)
java版直播商城平台规划及常见的营销模式有哪些?
GCC -fno-builtin & 内建函数
Spring 更简单的读取和存储对象
Django模版层
HIL 测试
我要写整个中文互联网界最牛逼的JVM系列教程 | 「类加载子系统」章节:ClassLoader的常用方法及其获取方法
vue3 + vite 性能优化 ( 从5s -> 0.5s )
时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测
-
原文地址:https://blog.csdn.net/u013250861/article/details/128089049