The multiset::emplace_hint() is a built-in function in C++ STL which inserts a new element in the multiset. 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 multiset container only.
Syntax:
multiset_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 multiset container only.
- value: This specifies the element to inserted in the multiset container.
Return Value: The function returns an iterator pointing to the position where the insertion is done.
Below program illustrates the above function.
Program 1:
// CPP program to demonstrate the // multiset::emplace_hint() function #include <bits/stdc++.h> using namespace std; int main() { multiset<int> s; auto it = s.emplace_hint(s.begin(), 1); // stores the position of 2's insertion it = s.emplace_hint(it, 2); // fast step as it directly // starts the search step from // position where 2 was last inserted s.emplace_hint(it, 4); // this is a slower step as // it starts checking from the // position where 4 was inserted // but 3 is to be inserted before 4 s.emplace_hint(it, 3); // prints the multiset elements for (auto it = s.begin(); it != s.end(); it++) cout << *it << " "; return 0; } |
1 2 3 4
Program 2:
// CPP program to demonstrate the // multiset::emplace_hint() function #include <bits/stdc++.h> using namespace std; int main() { multiset<int> s; auto it = s.emplace_hint(s.begin(), 1); // stores the position of 2's insertion it = s.emplace_hint(it, 2); // fast step as it directly // starts the search step from // position where 2 was last inserted s.emplace_hint(it, 4); // this is a slower step as // it starts checking from the // position where 4 was inserted // but 3 is to be inserted before 4 s.emplace_hint(it, 3); // slower steps s.emplace_hint(s.begin(), 6); s.emplace_hint(s.begin(), 6); s.emplace_hint(s.begin(), 6); s.emplace_hint(s.begin(), 6); // prints the multiset elements for (auto it = s.begin(); it != s.end(); it++) cout << *it << " "; return 0; } |
1 2 3 4 6 6 6 6
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- multiset count() function in C++ STL
- multiset equal_range() function in C++ STL
- multiset find() function in C++ STL
- multiset clear() function in C++ STL
- multiset key_comp() function in C++ STL
- multiset empty() function in C++ STL
- multiset begin() and end() function in C++ STL
- multiset insert() function in C++ STL
- multiset get_allocator() function in C++ STL
- multiset crbegin() and crend() function in C++ STL
- multiset rbegin() and rend() function in C++ STL
- multiset cbegin() and cend() function in C++ STL
- multiset erase() in C++ STL
- multiset max_size() in C++ STL
- multiset::emplace() in C++ STL
- multiset::operator= in C++ STL
- multiset::swap() in C++ STL
- multiset upper_bound() in C++ STL with Examples
- multiset max_size() in C++ STL with Examples
- multiset lower_bound() in C++ STL with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

