 st8b7c:f320:99b9:690f:4595:cd17:293a:c069set<Key,Compare,Allocator>8b7c:f320:99b9:690f:4595:cd17:293a:c069mplace    template< class... Args >
st8b7c:f320:99b9:690f:4595:cd17:293a:c069pair<iterator,bool> emplace( Args&&... args );
    (since C++11)    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 st8b7c:f320:99b9:690f:4595:cd17:293a:c069forward<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.
 Parameters    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 if insertion happened, false if it did not).
 Exceptions If an exception is thrown by any operation, this function has no effect (strong exception guarantee).
 Complexity Logarithmic in the size of the container.
 Example  #include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
 
class Dew
{
  private:
    int a;
    int b;
    int c;
 
  public:
    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() {
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069set<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() {
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069set<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(st8b7c:f320:99b9:690f:4595:cd17:293a:c069function<int()> set_test, st8b7c:f320:99b9:690f:4595:cd17:293a:c069string what = "") {
  auto start = st8b7c:f320:99b9:690f:4595:cd17:293a:c069chrono8b7c:f320:99b9:690f:4595:cd17:293a:c069system_clock8b7c:f320:99b9:690f:4595:cd17:293a:c069now();
  int setsize = set_test();
  auto stop = st8b7c:f320:99b9:690f:4595:cd17:293a:c069chrono8b7c:f320:99b9:690f:4595:cd17:293a:c069system_clock8b7c:f320:99b9:690f:4595:cd17:293a:c069now();
  st8b7c:f320:99b9:690f:4595:cd17:293a:c069chrono8b7c:f320:99b9:690f:4595:cd17:293a:c069uration<double, st8b7c:f320:99b9:690f:4595:cd17:293a:c069milli> time = stop - start;
  if (what.size() > 0 && setsize > 0) {
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << st8b7c:f320:99b9:690f:4595:cd17:293a:c069fixed << st8b7c:f320:99b9:690f:4595:cd17:293a:c069setprecision(2)
        << time.count() << "  ms for " << what << '\n';
  }
}
 
int main()
{
  set_insert();
  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
(C++11)   constructs elements in-place using a hint  (public member function) 
    insert   inserts elements or nodes (since C++17)  (public member function) 
  

    
{'path': 'public/docs/cpp/container/set/emplace.html', 'source': 'DevDocs.IO'}