C++ Unordered_set:: emplace_hint() Function



TheC++ std::unordered_set::emplace_hint() function is used to insert a new element using hints or position as a suggestion of where the element should go in the unordered_set if its value is unique and increases the container size by one.

This function allows the new element to be inserted at the given position while avoiding unnecessary copy or move operations. The element may only be inserted if the element does not exists in the unordered_set container.

Syntax

Following is the syntax of std::unordered_set::emplace_hint() function.

iterator emplace_hint ( const_iterator position, Args&&... args );

Parameters

  • position − It specifies the hint for the position.
  • element − It indicates the arguments passed to the sonstructor.

Return Value

This function returns an iterator pointing to the insert element if the element is not present in the unordered_set. Otherwise, it returns an iterator pointing to the already-present element. if the element is already present in the unordered_set.

Example 1

Let's look at the following example, where we are going to demonstrate the usage of unordered_set::emplace_hint() function.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<string> myUset;

   myUset.emplace_hint(myUset.begin(), "kittu");
   myUset.emplace_hint(myUset.begin(), "prasad");
   myUset.emplace_hint(myUset.begin(), "sairamkrishna");

   cout <&lt "myUset containing:";
   for (const string& x: myUset) cout <&lt "\n" <&lt x;

   cout <&lt endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

myUset containing:
sairamkrishna
prasad
kittu

Example 2

Consider the following example, where we are going to use the emplace_hint() function and inserting the element at the given position.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<int> myUset={10, 20};

   myUset.emplace_hint(myUset.end(), 10);
   myUset.emplace_hint(myUset.begin(), 40);
   myUset.emplace_hint(myUset.end(), 50);

   cout <&lt "myUset containing:";
   for (const int& x: myUset)
      cout <&lt " " <&lt x;
   cout <&lt endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

myUset containing: 50 40 20 10

Example 3

In the following example, we are going to insert the element at the given position using the emplace_hint() function and checking whether we can insert the element that is already available.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<int> myUset={10, 20, 30, 40};

   myUset.emplace_hint(myUset.end(), 30);
   myUset.emplace_hint(myUset.end(), 40);

   cout <&lt "myUset containing:";
   for (const int& x: myUset)
      cout <&lt " " <&lt x;
   cout <&lt endl;
   return 0;
}

Output

Following is the output of the above code −

myUset containing: 40 30 20 10

Example 4

Following is the example, where we are going to consider the unordered_set of char type and inserting the element at the beginning using the emplace_hint() function.

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
   unordered_set<char> myUset{'a', 'b', 'c', 'd'}; 
   unordered_set<char>::iterator it;

   it = myUset.begin();
   myUset.emplace_hint(it, 'e');
   it = myUset.emplace_hint(myUset.end(), 'f');

   cout<&lt"myUset contains: ";
   for(it = myUset.begin(); it != myUset.end(); ++it)
      cout<&lt*it<&lt" ";

   return 0;
}

Output

Output of the above code is as follows −

myUset contains: f e d c b a