**unordered_map的赋值方式
代码:
代码中有注释。
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<int, int> map;
//使用insert插入
map.insert(pair<int, int>(1, 10));
map.insert(pair<int, int>(2, 20));
map.insert(pair<int, int>(3, 30));
map.insert(pair<int, int>(4, 40));
map.insert(pair<int, int>(1, 100));
//使用“数组”形式插入
map[2] = 200;
//两种插入方式的区别:用insert插入有相同的键值,插入的value不会被覆盖;而使用[]插入,若有相同的键值,则原来的value会被覆盖。
//打印
for (auto &x : map) {
cout << x.first << " " << x.second << endl;
}
cout << map[1] << endl;
cout << map[2] << endl;
return 0;
}
结果: