Skip to content
Related Articles

Related Articles

unordered_map emplace_hint() function in C++ STL

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Sep, 2022
View Discussion
Improve Article
Save Article

The unordered_map::emplace_hint() is a built-in function in C++ STL which inserts the key and its element in the unordered_map container with a given hint. It effectively increases the container size by one as unordered_map is the container that stores keys with the element value. The hint provided does not affect the position to be entered, it only increases the speed of insertion as it points to the position from where the search for the ordering is to be started. It inserts in the same order which is followed by the container. It works similarly to unordered_map::emplace() function but is at times faster than it if the user provides position accurately. It does not insert the key with the element if it is already present in the map container as the map stores unique key only. Syntax:

unordered_map_name.emplace_hint(position, key, element)

Parameters: The function accepts the following parameters which are described below.

  • position: specifies the position from where the search operation for the ordering is to be started, hence making the insertion faster.
  • key: specifies the key to be inserted in the unordered_map container.
  • element: specifies the element to the key which is to be inserted in the unordered_map container.

Return Type: This function does not return anything. Time Complexity: O(n) in worst case. Below programs illustrate emplace_hint() method: Example 1: 

CPP




// C++ program to illustrate the
// unordered_map::emplace_hint() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    unordered_map<int, int> mp;
 
    // insert elements in random order
    mp.emplace_hint(mp.begin(), 2, 30);
    mp.emplace_hint(mp.begin(), 1, 40);
    mp.emplace_hint(mp.begin(), 3, 60);
 
    // prints the elements
    cout << "\nThe unordered_map is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); itr++)
        cout << itr->first << "\t"
             << itr->second << endl;
 
    return 0;
}


Output:

The unordered_map is : 
KEY    ELEMENT
3    60
2    30
1    40

Example 2 

CPP




// C++ program to illustrate the
// unordered_map::emplace_hint() function
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    unordered_map<char, int> mp;
 
    // insert elements in random order
    mp.emplace_hint(mp.begin(), 'b', 30);
    mp.emplace_hint(mp.begin(), 'a', 40);
    mp.emplace_hint(mp.begin(), 'c', 60);
 
    // prints the elements
    cout << "\nThe unordered_map is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); itr++)
        cout << itr->first << "\t"
             << itr->second << endl;
 
    return 0;
}


Output:

The unordered_map is : 
KEY    ELEMENT
c    60
b    30
a    40

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!