C++ Multimap 库 - emplace() 函数
描述
C++ 函数 std::multimap::emplace() 通过插入新元素来扩展容器。
此成员函数将 multimap 的大小增加一。
声明
以下是 std::multimap::emplace() 函数形式 std::map 头的声明。
C++11
template <class... Args> iterator emplace (Args&&... args);
参数
args − 参数转发给元素的构造函数。
返回值
将迭代器返回到新插入的元素。
异常
如果抛出异常,对容器没有影响。
时间复杂度
Logarithmic i.e. O(log n)
示例
以下示例显示了 std::multimap::emplace() 函数的用法。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
multimap<char, int> m {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5},
};
m.emplace('a', 2);
m.emplace('b', 2);
cout << "Multimap contains following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Multimap contains following elements a = 1 a = 2 b = 2 b = 2 c = 3 d = 4 e = 5

