unordered_map::insert
描述 (Description)
C ++函数std::unordered_map::insert()通过在unordered_map中插入新元素来扩展容器。
声明 (Declaration)
以下是std :: unordered_map :: insert()函数形式std :: unordered_map头的声明。
C++11
template <class InputIterator>
void insert (InputIterator first,InputIterator last);
参数 (Parameters)
first - 将迭代器输入到范围内的初始位置。
last - 将迭代器输入到范围内的最终位置。
返回值
没有
时间复杂
线性即平均情况下为O(n)。
在最坏的情况下,二次即O(N *(大小+ 1))。
例子 (Example)
以下示例显示了std :: unordered_map :: insert()函数的用法。
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_map<char, int> um1 = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5},
};
unordered_map<char, int> um2;
um2.insert(um1.begin(), um1.end());
cout << "Unordered map contains following elements" << endl;
for (auto it = um2.begin(); it != um2.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Unordered map contains following elements
d = 4
c = 3
b = 2
a = 1
e = 5