本篇学习unordered_map的插入数据操作,具体的函数如下:
(C++11) | 插入元素或结点 (C++17 起) (公开成员函数) |
(C++17) | 插入元素,或若键已存在则赋值给当前元素 (公开成员函数) |
(C++11) | 原位构造元素 (公开成员函数) |
(C++11) | 使用提示原位构造元素 (公开成员函数) |
(C++17) | 若键不存在则原位插入,若键存在则不做任何事 (公开成员函数) |
示例代码:
#include <unordered_map>
#include <string>
#include <time.h>
#include <iostream>
using namespace std;
void insertOperator()
{
unordered_map<int, string>map1;
//1.insert 插入元素或结点
map1.insert({1, "hello"});
auto map1it = map1.begin();
map1.insert(map1it, {7, "333333"});//键值存在插入无效
map1.insert(pair<int, string>(3, "scott"));
map1.insert(pair(2, "C++17 的写法"));//C++17 的写法
//map1[2] = "修改元素";//元素修改
cout << "map1.size = " << map1.size() << " map1.max_size = " << map1.max_size() << endl;
auto iter1 = map1.begin();
cout << "map1的值为:" << endl;
while (iter1 != map1.end())
{
cout << iter1->first << ": " << iter1->second << endl;
iter1++;
}
//2.insert_or_assign 插入元素,或若键已存在则赋值给当前元素
map1.insert_or_assign(3, "猴子");//C++17 键值存在,修改当前值
map1.insert_or_assign(4, "狮泉河");//C++17 键值不存在,插入当有值
cout << "map1.size = " << map1.size() << " map1.max_size = " << map1.max_size() << endl;
auto iter2 = map1.begin();
cout << "map1的值为:" << endl;
while (iter2 != map1.end())
{
cout << iter2->first << ": " << iter2->second << endl;
iter2++;
}
//3.emplace原位构造元素
unordered_map<int, string>map2;
map2.emplace(11, "emplace插入第一个元素");
map2.emplace(11, "emplace插入11元素");//插入失败
map2.emplace(pair(12, "emplace插入第二个元素"));
auto pair1 = map2.emplace(pair{13, "emplace插入第三个元素"});
//返回一个pair<iterator, bool>
cout << "iterator.first:" << pair1.first->first << " second: " << pair1.second << endl;
cout << "map2.size = " << map2.size() << " map2.max_size = " << map2.max_size() << endl;
auto map2it = map2.begin();
while(map2it != map2.end())
{
cout << map2it->first << ": " << map2it->second << endl;
++map2it;
}
cout << "---------------------------------" << endl;
//4.emplace_hint使用提示原位构造元素
unordered_map<int, string> map3;
auto map3it = map3.begin();
//返回一个迭代器
auto map3Hint1 = map3.emplace_hint(map3it, 23, "emplace_hint插入第三个元素");
map3.emplace_hint(map3it, 21, "emplace_hint插入第一个元素");
auto map3Hint2 = map3.emplace_hint(map3it, 21, "emplace_hint插入第二个元素");//插入失败
unordered_map<int, string>::iterator map3it2 = map3.begin();
while(map3it2 != map3.end())
{
cout << map3it2->first << ": " << map3it2->second << endl;
++map3it2;
}
cout << "emplace_hint1 " << map3Hint1->first << ": " << map3Hint1->second << endl;
cout << "emplace_hint2 " << map3Hint2->first << ": " << map3Hint2->second << endl;
//输出的都是按顺序的,那这种插入还有什么意义?
//5.try_emplace若键不存在则原位插入,若键存在则不做任何事 C++17
map3.try_emplace(24, "try_emplace插入第一个元素");
map3.try_emplace(24, "try_emplace插入第二个元素");
unordered_map<int, string>::iterator map3it3 = map3.begin();
while(map3it3 != map3.end())
{
cout << map3it3->first << ": " << map3it3->second << endl;
++map3it3;
}
//插入时间对比
int count = 1000000;
unordered_map<int, string> map4;
double startTime1 = clock();//1计算开始
for(int i = 0; i < count; i++)//214748
{
map4.insert(pair{i, "hello"});
}
double endTime1 = clock();//1时间结束
map4.clear();
cout << "for 1 run time is: " << (double)(endTime1 - startTime1) / CLOCKS_PER_SEC << "s" << endl;
cout << endl;
unordered_map<int, string> map5;
double startTime2 = clock();//1计算开始
for(int i = 0; i < count; i++)//214748
{
map5.emplace(i, "hello");
}
double endTime2 = clock();//1时间结束
map5.clear();
cout << "for 2 run time is: " << (double)(endTime2 - startTime2) / CLOCKS_PER_SEC << "s" << endl;
cout << endl;
}
int main()
{
insertOperator();
cout << "hello world" << endl;
return 0;
}
运行结果:
参考:
https://zh.cppreference.com/w/cpp/container/unordered_map
http://www.cplusplus.com/reference/unordered_map/unordered_map/