emplace_hint() in Set C++ STL


Reading time: 20 minutes | Coding time: 5 minutes

Emplace_hint() is a method of Set container in C++ STL to insert unique elements in a set at a desired position while maintaining the ascending order property of the set.

Set is a container that is used to store the values which are unique i.e no value in a set can be repeated. If you want to edit the value added by you the only way is to remove the wrong element and add the correct one.

set::emplace_hint()

emplace_hint() is used to insert an unique element in a set just like emplace().

The only difference is that emplace_hint() not only takes the value to be inserted but also takes the desired position in which it should be inserted while keeping the property of set i.e in ascending order.

Syntax:

setname.emplace_hint(iterator position, value) 

Parameters : The function has 2 paramenters :

  • position: It contains the desired position where the value need to be inserted.
  • value: It contains the element to inserted in the set.

Returns : The function returns an iterator pointing to the position where the insertion is performed.

Example 1

Go through this C++ example carefully. Following it, we will explain the points explained in the code.

#include <iostream> 
#include <set> 
using namespace std; 
  
int main() 
{ 
    set<int> s; 
    auto it = s.emplace_hint(s.begin(), 2); 
  
    // stores the position of 2's insertion 
    it = s.emplace_hint(it, 3);
    s.emplace_hint(it, 4); 
  
    // this is a slower step as checking starts from the position where 3 was inserted but 1 is to be inserted before 2 
    s.emplace_hint(it, 1); 
    s.emplace_hint(it, 5); 
    s.emplace_hint(it, 5); // Already exsisting 
    s.emplace_hint(it, 6); 
    
for (auto it = s.begin(); it != s.end(); it++) 
        cout << *it << " "; 
    return 0; 
} 

Output:

1 2 3 4 5 6

In the above example it can be noticed that even though we insert a lower value than already inserted one the value to be inserted maintains it position (by default ascending) and if we insert an exsisting value then the positon of the already prsent value is returned.

Hence the output is in ascedning order irrespective of the order in which they are inserted.

Example 2

Go through this C++ example carefully. Following it, we will explain the points explained in the code.

#include <iostream> 
#include <set> 
using namespace std; 
  
int main() 
{ 
  
    int mul = 1; 
    set<int> s; 
    auto it = s.emplace_hint(s.begin(), 2);
    it = s.emplace_hint(it, 3);
    s.emplace_hint(it, 4); 
    s.emplace_hint(it, 1); 
    s.emplace_hint(it, 5); 
  
    set<int>::iterator it; 
  
    while (!s.empty()) 
    { 
        it = s.begin(); 
        mul = mul*(*it); 
        s.erase(it); 
    } 
  
  
    cout << mul; 
    return 0; 
} 

Output:

120

In the above example we multiply the number present in set(in this case factorial of 5) by applying the iterator on the different position and storing the value after each iteration. The values are stored in the set as per example 1.

NOTE : If the element passed in the parameter is present already then it returns an iterator pointing to that particluar position.

NOTE : The new element is always inserted following the property of the set container only that are already explained in previous articles.

Difference between emplace and emplace_hint

  • emplace has 1 parameter while emplace_hint has 2 paramter.
  • emplace doesn't return any value while emplace_hint returns a value.