为了给map容器添加元素,我们有4种方法:1. 最简洁的方式就是通过'[]',中括号内为key,通过map[key]=value的形式为map添加元素(这里如果已存在key,则进行覆盖);2. 然后就是大家熟悉的insert(),insert()在此处既可单个插入,又能多个插入,还可以通过迭代器参数优化从指定位置插入value值;3. 然后就是emplace()直接构造了,通过传入参数而非传入对象直接构造,但是仅能添加单个元素,在要求单个添加时效率优于insert();最后一种是emplace_hint(),从hint迭代器开始寻找插入位置,避免无效的搜索路径,当hint迭代器合理设置时插入效率最高,有返回值,返回类型为迭代器,如果插入成功则返回指向新元素的迭代器,否则返回指向已存在元素的迭代器。下面我们就通过代码演示这4种方法:
1. '[]'为map插入元素:
#include<bits/stdc++.h>//万能头
#include<string>
#include<map>//包含头文件,养成好习惯
using namespace std;
/*'[]'*/
void test()
{
map<string ,string> mp;
/*只能单个插入,重复key值会进行覆盖*/
mp["数据结构教程"] ="https://www.dotcpp.com/course/ds/" ;
mp["Java教程"] ="https://www.dotcpp.com/course/java/";
cout << "遍历map容器内所有键值对: \n";
for(map<string,string>::iterator it = mp.begin();it!=mp.end();++it)cout << "key=" <<setw(15)<< left << it->first << "value=" << it->second << '\n';
cout << '\n' ;
cout << "通过'[]'插入元素如果重复key值会覆盖: \n";
mp["数据结构教程"] ="dotcpp.com" ;
mp["Java教程"] ="dotcpp.com" ;
for(map<string,string>::iterator it = mp.begin();it!=mp.end();++it)cout << "key=" <<setw(15)<< left << it->first << "value=" << it->second << '\n';
}
int main(){
test();
return 0;
}编译结果如下:
!['[]' '[]'](/assets/addons/ueditor/php/upload/image/20251016/1760607293590522.png)
成功编译,通过'[]'为map插入元素,如果重复key值则会进行覆盖。
2. insert()为map插入元素:
#include<bits/stdc++.h>//万能头
#include<string>
#include<map>//包含头文件,养成好习惯
//返回一个pair<iterator,bool>值,first指向插入元素的迭代器,second是bool表示是否插入成功
using namespace std;
/*insert()*/
void test()
{
map<string ,string> mp;
/*单个元素插入*/
//多种构造方式,但我推荐{}最为简洁
mp.insert(make_pair("C语言网","Dotcpp.com"));
mp.insert({"数据结构教程","https://www.dotcpp.com/course/ds/"}) ;
mp.insert(pair<string,string>({"Java教程","https://www.dotcpp.com/course/java/"}));
mp.insert(mp.begin(),{"C++教程","https://www.dotcpp.com/course/cpp/"});//提供一个位置迭代器,加速排序,高效插入
cout << "遍历map容器内所有键值对: \n";
for(map<string,string>::iterator it = mp.begin();it!=mp.end();++it)cout << "key=" <<setw(15)<< left << it->first << "value=" << it->second << '\n';
cout << '\n' ;
mp.clear();//清空操作
/*多个元素插入*/
mp.insert({
{"Python教程","https://www.dotcpp.com/course/python/"},
{"C++教程","https://www.dotcpp.com/course/cpp/"},
{"C语言网","Dotcpp.com"},
});
//添加已知map容器内所有元素
map<string ,string> mp_exist{{"Java教程","https://www.dotcpp.com/course/java/"},{"Linux命令","https://www.dotcpp.com/course/linuxcmd/"}};
mp.insert(mp_exist.begin(),mp_exist.end()) ;
cout << "遍历map容器内所有键值对: \n";
for(map<string,string>::iterator it = mp.begin();it!=mp.end();++it)cout << "key=" <<setw(15)<< left << it->first << "value=" << it->second << '\n';
}
int main(){
test();
return 0;
}编译结果如下:

成功编译,通过insert()为map插入单个或多个元素。
3. emplace()为map插入单个元素与insert()插入单个元素不一样,因为emplace()是传入参数,insert()是构建对象,而且通过emplace()直接构造避免了不必要的拷贝和移动函数。
map<string ,string> mp;//返回一个bool值,插入成功为true,反之为false
mp.emplace("C语言网","Dotcpp.com");//传入参数而非构造4. emplace_hint()为map插入单个元素,当hint迭代器正确设置时效率最高,比如为map插入1000000个元素:
/*使用emplace_hint()不管是否成功插入,都会返回有效的元素迭代器*/ map<int,int> mp; auto hint = mp.begin(); //for(int i=0;i<1000000;++i)mp.emplace(i,i+1); /*Process exited after 1.093 seconds with return value 0*/ /*(该结果表示程序运行时长 1.093秒)*/ //for(int i=0;i<1000000;++i)hint=mp.emplace_hint(hint,i,i+1); /*Process exited after 0.4466 seconds with return value 0*/ /*(该结果表示程序运行时长 0.4466秒)*/
由此可见,配合好迭代器使用emplace_hint能够最高效插入元素。
总结:4种插入方法各有特点,单个插入用'[]'最好记住,但是需谨慎其覆盖功能,以免破坏数据完整性;单个插入效率最高的是emplace;如果需要多个插入只能用insert();emplace_hint()插入元素需要巧用迭代器。
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程