C++-STL-map:map插入元素的几种方式【用数组方式插入数据】【用insert函数插入pair数据】【用insert函数插入value_type数据】

本文介绍了C++中map插入元素的三种方法:使用insert函数插入pair、插入value_type以及使用数组方式插入。强调了在数据插入时,insert函数会遵循键的唯一性,而数组方式可以覆盖已有键的值。同时,文章通过示例展示了如何检查insert操作是否成功,并提供了演示代码。最后,对比了数组插入时数据覆盖的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、第一种:用insert函数插入pair数据

#include <map>
#include <string>
#include <iostream>
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<<iter->first<<" "<<iter->second<<end;
       }
}

第二种:用insert函数插入value_type数据

#include <map>
#include <string>
#include <iostream>
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<<iter->first<<" "<<iter->second<<end;
       }
}

第三种:用数组方式插入数据

#include <map>
#include <string>
#include <iostream>
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<<iter->first<<" "<<iter->second<<end;
       }
}

以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用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<map<int, string>::iterator, bool> Insert_Pair;

Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));

我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。 下面给出完成代码,演示插入成功与否问题:

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int, string> mapStudent;
       pair<map<int, string>::iterator, bool> Insert_Pair;
       Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));
       if(Insert_Pair.second == true)
       {
              cout<<"Insert Successfully"<<endl;
       }
       else
       {
              cout<<"Insert Failure"<<endl;
       }
       Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
       if(Insert_Pair.second == true)
       {
              cout<<"Insert Successfully"<<endl;
       }
       else
       {
              cout<<"Insert Failure"<<endl;
       }
       map<int, string>::iterator  iter;
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
       {
           cout<<iter->first<<" "<<iter->second<<end;
       }
}

大家可以用如下程序,看下用数组插入在数据覆盖上的效果

#include <map>
#include <string>
#include <iostream>
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++)
       {
            cout<<iter->first<<" "<<iter->second<<end;
       }
}

C++ map的三种不同插入元素方法_zpznba的博客-CSDN博客_c++ map 添加元素

c++ map容器的元素插入_高凌霄的博客-CSDN博客_c++ map添加元素

C++map容器插入数据的4种方法_LaugustusJ的博客-CSDN博客_c++ map添加元素的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值