unordered_map emplace() in C++ STL

The unordered_map::emplace() is a built-in function in C++ STL which inserts the key and its element in the unordered_map container. It effectively increases the container size by one. If the same key is emplaced more than once, the map stores the first element only as the map is a container which does not store multiple keys of the same value.

How is it different from unordered_map insert()?
The advantage of emplace is, it does in-place insertion and avoids an unnecessary copy of object. For primitive data types, it does not matter which one we use. Please refer
this for details.


Syntax:

unordered_map_name.emplace(key, element)

Parameters: The function accepts two mandatory parameters which are described below:

  • key – specifies the key to be inserted in the multimap container.
  • element – specifies the element to the key which is to be inserted in the map container.

Return values: It returns a pair of an iterator and a bool. If the element already exists, it returns an iterator pointing to the already inserted element and if the element does not exist it returns an iterator to newly added container. The bool represents whether the insertion took place or not.

Below program illustrate the working of emplace function:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the illustration of
// unordered_map::emplace() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // initialize container
    unordered_map<int, int> mp;
  
    // insert elements in random order
    mp.emplace(2, 30);
    mp.emplace(1, 40);
    mp.emplace(2, 20);
    mp.emplace(1, 50);
    mp.emplace(4, 50);
  
    // prints the elements
    for (auto it = mp.begin(); it != mp.end(); it++)
        cout << it->first << "==>>"
             << it->second << "\n";
}

chevron_right


Output:

4==>>50
2==>>30
1==>>40

Example 2: Let’s say that we want to display all unique characters with there indices for a string. If a character appears multiple times then display the index at which it appeared first.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the illustration of
// unordered_map::emplace() function
  
#include <bits/stdc++.h>
  
using namespace std;
  
int main()
{
    string str = "geeksforgeeks";
  
    unordered_map<char, int> mp;
    for (int i = 0; i < str.length(); i++)
        mp.emplace(str[i], i);
  
    for (auto it = mp.begin(); it != mp.end(); it++)
        cout << it->first << "==>>" << it->second << "\n";
}

chevron_right


Output:

r==>>7
e==>>1
s==>>4
g==>>0
k==>>3
f==>>5
o==>>6

Explanation: “g” appeared 2 times in the string 1st at index “0” and then at index “8” but emplace function does not let us save in unordered_map until the given key is unique. That is why “0” is saved and “8” didn’t.
Similarly for “e”, “1” is saved and “2”, “9”, “10” didn’t.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.