为了给unordered_map容器添加元素,我们有4种方法:1. 最简洁的方式就是通过'[]',中括号内为key,通过unordered_map[key]=value的形式为容器添加元素(这里如果已存在key,则进行覆盖);2. 然后就是大家熟悉的insert(),insert()在此处既可单个插入,又能多个插入;3. emplace()直接传入参数构建,但是仅能添加单个元素;4. 最后一种是emplace_hint(),先由哈希函数分配桶位置,然后再判断hint是否有用,有返回值,返回类型为迭代器,如果插入成功则返回指向新元素的迭代器,否则返回指向已存在元素的迭代器。一般情况下(指负载因子低于0.75),这4种插入方式都是O(1)的复杂度,性能上不分高低,所以对于读者来说,哪种记忆简单哪种就最高效!下面我们就通过代码演示这4种方法:
1. '[]'为unordered_map容器插入元素:
#include<bits/stdc++.h>//万能头 #include<string> #include<unordered_map>//包含头文件,养成好习惯 using namespace std; /*'[]'*/ void test() { unordered_map<string ,string> ump; /*只能单个插入,重复key值会进行覆盖*/ ump["数据结构教程"] ="https://www.dotcpp.com/course/ds/" ; ump["Java教程"] ="https://www.dotcpp.com/course/java/"; cout << "遍历unordered_map容器内所有键值对: \n"; for(unordered_map<string,string>::iterator it = ump.begin();it!=ump.end();++it)cout << "key=" <<setw(15)<< left << it->first << "value=" << it->second << '\n'; cout << '\n' ; cout << "通过'[]'插入元素如果重复key值会覆盖: \n"; ump["数据结构教程"] ="dotcpp.com" ; ump["Java教程"] ="dotcpp.com" ; for(unordered_map<string,string>::iterator it = ump.begin();it!=ump.end();++it)cout << "key=" <<setw(15)<< left << it->first << "value=" << it->second << '\n'; cout << '\n' ; } int main(){ test(); return 0; }
编译结果如下:
成功编译,通过'[]'为unordered_map容器插入元素,如果重复key值则会进行覆盖。
2. insert()为unordered_map容器插入元素:
#include<bits/stdc++.h>//万能头 #include<string> #include<unordered_map>//包含头文件,养成好习惯 //返回一个bool值,插入成功为true,反之为false using namespace std; /*insert()*/ void test() { unordered_map<string ,string> ump; /*单个元素插入*/ //多种构造方式,但我推荐{}最为简洁 ump.insert(make_pair("C语言网","dotcpp.com")); ump.insert({"数据结构教程","https://www.dotcpp.com/course/ds/"}) ; ump.insert(pair<string,string>({"Java教程","https://www.dotcpp.com/course/java/"})); ump.insert(ump.begin(),{"C++教程","https://www.dotcpp.com/course/cpp/"});//提供一个位置迭代器,加速排序,高效插入 cout << "遍历unordered_map容器内所有键值对: \n"; for(unordered_map<string,string>::iterator it = ump.begin();it!=ump.end();++it)cout << "key=" <<setw(15)<< left << it->first << "value=" << it->second << '\n'; cout << '\n' ; ump.clear();//清空操作 /*多个元素插入*/ ump.insert({ {"Python教程","https://www.dotcpp.com/course/python/"}, {"C++教程","https://www.dotcpp.com/course/cpp/"}, {"C语言网","dotcpp.com"}, }); //添加已知unordered_map容器内所有元素 unordered_map<string ,string> ump_exist{{"Java教程","https://www.dotcpp.com/course/java/"},{"Linux命令","https://www.dotcpp.com/course/linuxcmd/"}}; ump.insert(ump_exist.begin(),ump_exist.end()) ; cout << "遍历unordered_map容器内所有键值对: \n"; for(unordered_map<string,string>::iterator it = ump.begin();it!=ump.end();++it)cout << "key=" <<setw(15)<< left << it->first << "value=" << it->second << '\n'; } int main(){ test(); return 0; }
编译结果如下:
成功编译,通过insert()为unordered_map插入单个或多个元素。
3. emplace()为unordered_map容器插入单个元素与insert()插入单个元素不一样,因为emplace()是传入参数,insert()是传入对象。
unordered_map<string ,string> ump;//返回一个bool值,插入成功为true,反之为false ump.emplace("C语言网","dotcpp.com");//传入参数而非构造
4. emplace_hint()为unordered_map容器插入单个元素,由于底层结构是哈希表,所以其效率远没有在map容器中插入元素那么高。
#include<bits/stdc++.h> using namespace std; int main() { /*配合迭代器插入*/ unordered_map<int,int> ump; ump.emplace_hint(ump.begin(),0,0); return 0; }
成功编译!
总结:4种插入方法各有特点,单个插入用'[]'最好记住,但是需谨慎其覆盖功能,以免破坏数据完整性;如果需要多个插入只能用inset();其余方法了解就好。
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程