C++ Standard Library C++ STL Library

C++ unordered_multiset - emplace() Function



The C++ unordered_multiset::emplace function is used to insert a new element in the unordered_multiset. The insertion of the new element increases the size of the unordered_multiset by one.

Syntax

template <class... Args>
iterator emplace (Args&&... args);

Parameters

args Arguments forwarded to construct the new element.

Return Value

Returns an iterator pointed to newly added element.

Time Complexity

Average case: Constant i.e, Θ(1).
Worst case: Linear i.e, Θ(n).

Example:

In the example below, the unordered_multiset::emplace function is used to insert new element in the unordered_multiset called uMSet.

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_multiset<int> uMSet{10, 20, 30, 40, 50};
  unordered_multiset<int>::iterator it; 

  //insert a new element in the unordered_multiset
  uMSet.emplace(50);
  uMSet.emplace(60);
  
  cout<<"uMSet contains: ";
  for(it = uMSet.begin(); it != uMSet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

uMSet contains: 60 10 20 30 40 50 50 

❮ C++ <unordered_set> Library