The unordered_set::emplace_hint() function is an inbuilt function in C++ STL which inserts a new element in the unordered_set only if the value to be inserted is unique, with a given hint.
Syntax:
unordered_set_name.emplace_hint( position, value )
Parameter: This function accepts two parameters as mentioned above and described below:
- position: This parameter is used to describe the position for inserted operation.
- value: This parameter is used to hold the which needs to insert.
Return Value: If the value is not present in the unordered_set, then the function inserts the value and returns an iterator pointing to the inserted element. Else, if the value is already present in the unordered_set, then the function returns the iterator pointing to that element.
Below program illustrates the unordered_set::emplace_hint() function in C++ STL:
Program 1:
CPP
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set< int > uset = { 20, 40, 50, 60 };
uset.emplace_hint(uset.begin(), 80);
cout << "uset: " ;
for ( auto it = uset.begin(); it != uset.end(); it++)
cout << *it << " " ;
}
|
Output:
uset: 80 20 40 50 60
Program 2:
CPP
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set< int > uset = { 20, 40, 50, 60 };
uset.emplace_hint(uset.begin(), 50);
cout << "uset: " ;
for ( auto it = uset.begin(); it != uset.end(); it++)
cout << *it << " " ;
}
|
Output:
uset: 60 50 40 20
Time complexity: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Jun, 2023
Like Article
Save Article