unordered_map::emplace_hint
描述 (Description)
C ++函数std::unordered_map::emplace_hint()使用提示作为元素的位置在unordered_map中插入一个新元素。
声明 (Declaration)
以下是std :: unordered_map :: emplace_hint()函数形式std :: unordered_map头的声明。
C++11
template <class... Args>
iterator emplace_hint(const_iterator position, Args&&... args);
参数 (Parameters)
position - 提示插入元素的位置。
args - 转发以构造新元素的参数。
返回值
返回新插入元素的迭代器。 如果由于已存在的元素而导致插入失败,则它会将迭代器返回到现有元素。
时间复杂
常数,即平均情况下为O(1)。
在最坏的情况下线性即O(n)。
例子 (Example)
以下示例显示了std :: unordered_map :: emplace_hint()函数的用法。
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_map<char, int> um = {
{'b', 2},
{'c', 3},
{'d', 4},
};
um.emplace_hint(um.end(), 'e', 5);
um.emplace_hint(um.begin(), 'a', 1);
cout << "Unordered map contains following elements" << endl;
for (auto it = um.cbegin(); it != um.cend(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Unordered map contains following elements
a = 1
e = 5
d = 4
b = 2
c = 3