Skip to content
Related Articles

Related Articles

Improve Article

unordered_map emplace() in C++ STL

  • Difficulty Level : Easy
  • Last Updated : 09 Jan, 2019

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:




// 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";
}
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.




// 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";
}
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.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :