C++ Standard Library C++ STL Library

C++ unordered_multiset - emplace_hint() Function



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

The hint is only used to speed up the insertion process considering the actual insertion point is either specified position or close to it.

Syntax

template <class... Args>
iterator emplace_hint (const_iterator position, Args&&... args);

Parameters

position Specify hint for the position where the element can be inserted.
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_hint 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; 

  it = uMSet.begin();
  uMSet.emplace_hint(it, 50);
  it = uMSet.emplace_hint(uMSet.end(), 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

5