![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_multimap::emplace_hint() Function
The C++ unordered_multimap::emplace_hint() function is used to insert a new element in unordered_multimap using a hint or position. The position only acts as a hint; it does not decide the position at which the insertion is to be done, and it extends the container size by one by inserting the new element. This function is similar to the emplace() function.
Syntax
Following is the syntax of unordered_map::emplace_hint() function.
iterator emplace_hint ( const_iterator position, Args&&... args );
Parameters
- position − Hint for the position to insert element.
- args − It specifies the arguments forwarded to construct the new element.
Return value
This function returns an iterator to the newly inserted element.
Example 1
In the following example, let's see the usage of the emplace_hint() function.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<char, int> umm = { {'b', 2}, {'c', 3}, {'d', 4}, }; umm.emplace_hint(umm.begin(), 'b', 2); umm.emplace_hint(umm.end(), 'e', 3); cout << "Unordered multimap contains following elements" << endl; for (auto it = umm.begin(); it != umm.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
If we run the above code it will generate the following output −
Unordered multimap contains following elements e = 3 b = 2 b = 2 c = 3 d = 4
Example 2
Consider the following example, where we are going to add key/values into the container at the starting and ending position.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<string, int> umm = {{"Aman", 490},{"Vivek", 485},{"Akash", 500},{"Sonam", 450}}; cout << "multimap contains following elements before" << endl; for (auto it = umm.begin(); it != umm.end(); ++it) cout << it->first << " = " << it->second << endl; cout<<"after use of the emplace_hint() function \n"; umm.emplace_hint(umm.begin(), "Vivek", 440); umm.emplace_hint(umm.end(), "Aman", 460); umm.emplace_hint(umm.end(), "Akash", 460); cout << "multimap contains following elements" << endl; for (auto it = umm.begin(); it != umm.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
Following is the output of the above code −
multimap contains following elements before Akash = 500 Vivek = 485 Sonam = 450 Aman = 490 after use of the emplace_hint() function multimap contains following elements Aman = 460 Aman = 490 Sonam = 450 Vivek = 440 Vivek = 485 Akash = 460 Akash = 500
Example 3
Let's look at the following example, where we are going to use the emplace_hint() and storing the key-value pairs in random order.
#include <iostream> #include <unordered_map> using namespace std; int main() { unordered_multimap<int, string> Ummap; auto it = Ummap.emplace_hint(Ummap.begin(), 1, "Jun"); it = Ummap.emplace_hint(it, 1, "January"); it = Ummap.emplace_hint(it, 2, "February"); it = Ummap.emplace_hint(it, 3, "March"); Ummap.emplace_hint(it, 3, "April"); Ummap.emplace_hint(it, 2, "May"); cout << "The unordered_multimap contains following element : \n"; cout << "KEY\tELEMENT"<<endl; for (auto itr = Ummap.begin(); itr != Ummap.end(); itr++) cout << itr->first << "\t"<< itr->second << endl; return 0; }
Output
Output of the above code is as follows −
The unordered_multimap contains following element : KEY ELEMENT 3 March 3 April 2 May 2 February 1 Jun 1 January