C++ Standard Library C++ STL Library

C++ multiset - emplace() Function



The C++ multiset::emplace function is used to insert a new element in the multiset. The insertion of the new element increases the size of the multiset by one. As a multiset is an ordered data container, hence it stores the new element in its respective position to keep the multiset sorted.

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

Logarithmic i.e, Θ(log(n)).

Example:

In the example below, the multiset::emplace function is used to insert a new element in the multiset called MyMSet.

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

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

  return 0;
}

The output of the above code will be:

MyMSet contains: 10 20 30 40 50 50 60 

❮ C++ <set> Library

5