The set::emplace_hint() is a built-in function in C++ STL which inserts a new element in the set. A position is passed in the parameter of the function which acts as a hint from where the searching operation starts before inserting the element at its current position. The position only helps the process to get faster, it does not decide where the new element is to be inserted. The new element is inserted following the property of the set container only.
Syntax:
set_name.emplace_hint(iterator position, value)
Parameters: The function accepts two mandatory parameters which are described below:
- position: This parameter acts as a hint from where the searching operation is done before inserting the element at its current position. The position only helps the process to be faster, it does not decide where the new element is to be inserted. The new element is inserted following the property of the set container only.
- value: This specifies the element to inserted in the set container. The value is inserted into the set if it is not present before.
Return Value: The function returns an iterator pointing to the position where the insertion is done. If the element passed in the parameter already exists, then it returns an iterator pointing to the position where the existing element is.
Below program illustrates the above function.
#include <bits/stdc++.h>
using namespace std;
int main()
{
set< int > s;
auto it = s.emplace_hint(s.begin(), 1);
it = s.emplace_hint(it, 2);
s.emplace_hint(it, 3);
s.emplace_hint(it, 0);
for ( auto it = s.begin(); it != s.end(); it++)
cout << *it << " " ;
return 0;
}
|
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 :
10 Jul, 2018
Like Article
Save Article