C++ multiset - emplace_hint() Function
The C++ multiset::emplace_hint function is used to insert a new element in the multiset with the hint of insertion position. 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.
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
Logarithmic i.e, Θ(log(n)).
Constant i.e, Θ(1) if the insertion point for the element is position.
Example:
In the example below, the multiset::emplace_hint 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; it = MyMSet.begin(); MyMSet.emplace_hint(it, 50); it = MyMSet.emplace_hint(MyMSet.end(), 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