目录

map::emplace

描述 (Description)

C ++函数std::map::emplace()通过插入新元素来扩展容器。

插入仅在密钥不存在时进行。

声明 (Declaration)

以下是std :: map :: emplace()函数形式std :: map头的声明。

C++11

template <class... Args>
pair<iterator,bool> emplace (Args&&... args);

参数 (Parameters)

args - 转发给元素构造函数的参数。

返回值

返回一个由bool组成的对,表示是否发生了插入,并将迭代器返回给新插入的元素。

异常 (Exceptions)

如果任何操作抛出异常,则此函数无效。

时间复杂

对数即log(n)

例子 (Example)

以下示例显示了std :: map :: emplace()函数的用法。

#include <iostream>
#include <map>
using namespace std;
int main(void) {
   /* Initializer_list constructor */
   map<char, int> m;
   m.emplace('a', 1);
   m.emplace('b', 2);
   m.emplace('c', 3);
   m.emplace('d', 4);
   m.emplace('e', 5);
   cout << "Map contains following elements in reverse order" << endl;
   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Map contains following elements in reverse order
a = 1
b = 2
c = 3
d = 4
e = 5
↑回到顶部↑
WIKI教程 @2018