public member function
<unordered_map>

std::unordered_multimap::emplace

template <class... Args> iterator emplace ( Args&&... args );
Construct and insert element
Inserts a new element in the unordered_multimap. This new element is constructed in place using args as the arguments for the element's constructor.

This effectively increases the container size by one.

A similar member function exists, insert, which either copies or moves existing objects into the container.

参数

args
Arguments used to construct a new object of the mapped type for the inserted element.
Arguments forwarded to construct the new element (of type pair<const key_type, mapped_type>).
This can be one of
- Two arguments: one for the key, the other for the mapped value.
- A single argument of a pair type with a value for the key as first member, and a value for the mapped value as second.
- piecewise_construct as first argument, and two additional arguments with tuples to be forwarded as arguments for the key value and for the mapped value respectivelly.
See pair::pair for more info.

返回值

指向新插入元素的迭代器。

成员类型iterator是一个 forward iterator 类型。

新元素的存储使用allocator_traits<allocator_type>::construct()分配,这在失败时可能会抛出异常(对于默认的 allocatorbad_alloc如果分配请求不成功,则抛出)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// unordered_multimap::emplace
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_multimap<std::string,std::string> myumm;

  myumm.emplace ("NCC-1701", "C. Pike");
  myumm.emplace ("NCC-1701", "J.T. Kirk");
  myumm.emplace ("NCC-1701-D", "J.L. Picard");
  myumm.emplace ("NCC-74656", "K. Janeway");

  std::cout << "myumm contains:" << std::endl;
  for (auto& x: myumm)
    std::cout << x.first << ": " << x.second << std::endl;

  std::cout << std::endl;
  return 0;
}

可能的输出
mymap contains:
NCC-1701: C. Pike
NCC-1701: J.T. Kirk
NCC-1701-D: J.L. Picard
NCC-74656: K. Janeway


复杂度

平均情况:常量。
最坏情况:容器大小的线性。
可能会触发 rehash(未包含)。

迭代器有效性

在大多数情况下,插入后容器中的所有迭代器都保持有效。唯一的例外是当容器的增长强制重新哈希时。在这种情况下,容器中的所有迭代器都将失效。

A rehash is forced if the new container size after the insertion operation would increase above its capacity threshold (calculated as the container's bucket_count multiplied by its max_load_factor).

在所有情况下,`unordered_multimap` 容器中元素的引用都保持有效,即使在 rehash 之后也是如此。

另见