public member function
<unordered_set>

std::unordered_multiset::emplace

template <class... Args> iterator emplace ( Args&&... args );
Construct and insert element
Inserts a new element in the unordered_multiset. 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.

Parameters

args
Arguments passed to the constructor of the a new element to be inserted.

Return value

An iterator to the newly inserted element.

Member type iterator is a forward iterator type.

All iterators in an unordered_multiset have const access to the elements: Elements can be inserted or removed, but not modified while in the container.

The storage for the new element is allocated using allocator_traits<allocator_type>::construct(), which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

Example

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

int main ()
{
  std::unordered_multiset<std::string> myums;

  myums.emplace ("milk");
  myums.emplace ("tea");
  myums.emplace ("coffee");
  myums.emplace ("milk");

  std::cout << "myums contains:";
  for (const std::string& x: myums) std::cout << " " << x;

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

Possible output:
myset contains: tea coffee milk milk


Complexity

Average case: constant.
Worst case: linear in container size.
May trigger a rehash (not included).

Iterator validity

On most cases, all iterators in the container remain valid after the insertion. The only exception being when the growth of the container forces a rehash. In this case, all iterators in the container are invalidated.

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).

References to elements in the unordered_multiset container remain valid in all cases, even after a rehash.

See also