std::set<Key,Compare,Allocator>::emplace (3) - Linux Manuals

std::set<Key,Compare,Allocator>::emplace: std::set<Key,Compare,Allocator>::emplace


std::set<Key,Compare,Allocator>::emplace - std::set<Key,Compare,Allocator>::emplace


template< class... Args > (since C++11)
std::pair<iterator,bool> emplace( Args&&... args );

Inserts a new element into the container constructed in-place with the given args if there is no element with the key in the container.
Careful use of emplace allows the new element to be constructed while avoiding unnecessary copy or move operations. The constructor of the new element is called with exactly the same arguments as supplied to emplace, forwarded via std::forward<Args>(args).... The element may be constructed even if there already is an element with the key in the container, in which case the newly constructed element will be destroyed immediately.
No iterators or references are invalidated.


args - arguments to forward to the constructor of the element

Return value

Returns a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place. True for Insertion, False for No Insertion.


If an exception is thrown by any operation, this function has no effect.


Logarithmic in the size of the container.


// Run this code

  #include <chrono>
  #include <functional>
  #include <iomanip>
  #include <iostream>
  #include <set>
  #include <string>

  class Dew
      int a;
      int b;
      int c;

      Dew(int _a, int _b, int _c)
        : a(_a), b(_b), c(_c)

      bool operator<(const Dew &other) const
        if (a < other.a)
          return true;
        if (a == other.a && b < other.b)
          return true;
        return (a == other.a && b == other.b && c < other.c);

  const int nof_operations = 120;

  int set_emplace() {
      std::set<Dew> set;
      for(int i = 0; i < nof_operations; ++i)
          for(int j = 0; j < nof_operations; ++j)
              for(int k = 0; k < nof_operations; ++k)
                set.emplace(i, j, k);

      return set.size();

  int set_insert() {
      std::set<Dew> set;
      for(int i = 0; i < nof_operations; ++i)
          for(int j = 0; j < nof_operations; ++j)
              for(int k = 0; k < nof_operations; ++k)
                set.insert(Dew(i, j, k));

      return set.size();

  void timeit(std::function<int()> set_test, std::string what = "") {
    auto start = std::chrono::system_clock::now();
    int setsize = set_test();
    auto stop = std::chrono::system_clock::now();
    std::chrono::duration<double, std::milli> time = stop - start;
    if (what.size() > 0 && setsize > 0) {
      std::cout << std::fixed << std::setprecision(2)
          << time.count() << " ms for " << what << '\n';

  int main()
    timeit(set_insert, "insert");
    timeit(set_emplace, "emplace");
    timeit(set_insert, "insert");
    timeit(set_emplace, "emplace");

Possible output:

  638.45 ms for insert
  619.44 ms for emplace
  609.43 ms for insert
  652.55 ms for emplace

See also

emplace_hint constructs elements in-place using a hint
             (public member function)
             inserts elements
             or nodes
insert (since C++17)
             (public member function)