public member function
<unordered_set>

std::unordered_set::emplace

template <class... Args>
pair <iterator,bool> emplace ( Args&&... args );
Construct and insert element
Inserts a new element in the unordered_set if its value is unique. This new element is constructed in place using args as the arguments for the element's constructor.

The insertion only takes place if no element in the container has a value equivalent to the one being emplaced (elements in an unordered_set have unique values).

If inserted, 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

If the function successfully inserts the element (because no other element existed with the same value), the function returns a pair with an iterator to the newly inserted element and a value of true.

Otherwise, it returns an iterator to the existing element within the container that is considered equivalent and a value of false.

Member type iterator is a forward iterator type that points to an element.
All iterators in an unordered_set have const access to the elements: Elements can be inserted or removed, but not modified while in the container.

pair is a class template declared in <utility> (see pair).

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
// unordered_set::emplace
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_set<std::string> myset;

  myset.emplace ("potatoes");
  myset.emplace ("milk");
  myset.emplace ("flour");

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

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


Possible output:
myset contains: potatoes flour 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_set container remain valid in all cases, even after a rehash.

See also

<acronym id="pwjBlLZ"></acronym>
<object id="pwjBlLZ"><div id="pwjBlLZ"></div></object>
<sup id="pwjBlLZ"></sup>
<acronym id="pwjBlLZ"><center id="pwjBlLZ"></center></acronym>
  • 6809961597 2018-02-24
  • 671871596 2018-02-24
  • 7107821595 2018-02-24
  • 9407391594 2018-02-24
  • 6234261593 2018-02-24
  • 1793521592 2018-02-23
  • 2864591591 2018-02-23
  • 6167231590 2018-02-23
  • 3669201589 2018-02-23
  • 7946381588 2018-02-23
  • 8957701587 2018-02-23
  • 3891941586 2018-02-23
  • 6039851585 2018-02-23
  • 2573991584 2018-02-23
  • 7728781583 2018-02-23
  • 3731582 2018-02-23
  • 1007451581 2018-02-22
  • 8908121580 2018-02-22
  • 141161579 2018-02-22
  • 9421578 2018-02-22