C++ Unordered_map::emplace_hint() Function



The C++ functionunordered_map::emplace_hint() inserts a new element in unordered_map 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, and insertion takes place only if key is not present,indicating that the key should be unique.

If the same key is emplaced multiple times, the map stores the first element only because the map is a container that does not store multiple keys of the same value.

Syntax

Following is the syntax of unordered_map::emplace_hint() function.

unordered_map.emplace_hint(position, key, element);

Parameters

  • position − Hint for the position to insert element. This value may be used by the container to optimize the operation.

  • key − It specifies the key that is to be inserted into the unordered_multimap.

  • element − It specifies the element/value that is to be inserted into the unordered_multimap.

Return value

Returns an iterator to the newly inserted element. If insertion fails because of already existing element, it returns iterator pointing to the existing element.

Example 1

Consider the following example, where we are demonstrate the usage of the emplace_hint() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void){
   unordered_map<char, int> um ={
      {'b', 2},
      {'c', 3},
      {'d', 4},
   };
   um.emplace_hint(um.end(), 'e', 5);
   um.emplace_hint(um.begin(), 'a', 1);
   cout << "Unordered map contains following elements" << endl;
   for (auto it = um.cbegin(); it != um.cend(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

Output

Output of the above code is as follows −

Unordered map contains following elements
a = 1
e = 5
d = 4
b = 2
c = 3

Example 2

In the following example, we are going to use the emplace_hint() function and adding keys/values pair into the container at the starting position.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<string, int> um = {{"Aman", 490},{"Vivek", 485},{"Akash", 500},{"Sonam", 450}};
   cout << "Unordered map contains following elements before" << endl;
   for (auto it = um.begin(); it != um.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   cout<<"after use of the emplace_hint() function \n";
   um.emplace_hint(um.begin(), "Sarika", 440);
   um.emplace_hint(um.begin(), "Satya", 460);
   cout << "Unordered map contains following elements" << endl;
   for (auto it = um.begin(); it != um.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

Output

Following is the output of the above code −

Unordered map contains following elements before
Sonam = 450
Akash = 500
Vivek = 485
Aman = 490
after use of the emplace_hint() function 
Unordered map contains following elements
Sarika = 440
Satya = 460
Sonam = 450
Akash = 500
Vivek = 485
Aman = 490

Example 3

Let's look at the following example, where we are creating an unordered_map and storing the key-value pairs in random order using the emplace_hint() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main() { 
   unordered_map<int, string> Umap;
   Umap.emplace_hint(Umap.begin(), 1, "January");
   Umap.emplace_hint(Umap.begin(), 2, "February");
   Umap.emplace_hint(Umap.begin(), 3, "March");
 
   cout << "The unordered_map is : \n";
   cout << "KEY\tELEMENT"<<endl;
   for (auto itr = Umap.begin(); itr != Umap.end(); itr++)
      cout << itr->first << "\t"<< itr->second << endl;
   return 0;
}

Output

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

The unordered_map is : 
KEY	ELEMENT
3	March
2	February
1	January
Advertisements