emplace() in Set C++ STL


Reading time: 20 minutes | Coding time: 5 minutes

Emplace is a function of set container in C++ STL which is used to insert elements in the set. It is considered to be a faster alternative to insert().

Set is a container that is used to store the values which are unique i.e no value in a set can be repeated and no value can be repeated. If you want to edit the value added by you the only way is to remove the wrong element and add the correct one.

set::emplace(): inserting elements in set

The function is used to insert an element in the set container, if and only if the element to be inserted is unique i.e does not already exists in the set. In fact, emplace() can be used to insert pair of values which is not possible in insert() function directly.

Emplace was introduced in C++11 and is considered to more faster and advised to be used when your object is non-trival that is user defined objects.

Syntax:

setname.emplace(value)
  • Parameters : The element to be inserted is passed as the parameter.
  • Result : The unique parameter is added. No return value.

Example 1

In this C++ example, we will insert integer values in a set using emplace() and see that inserting duplicate values have not impact.

#include <iostream> 
#include <set> 
using namespace std; 
  
int main() 
{ 
    set<int> myset{}; 
    myset.emplace(1); 
    myset.emplace(2); 
    myset.emplace(3); 
    myset.emplace(4); 
    myset.emplace(5); 

    myset.emplace(6);// adding unique element
    myset.emplace(2); //already present
  
    for (auto it = myset.begin(); 
         it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
} 

Output:

1 2 3 4 5 6

Example 2

In this C++ example, we will insert strings in a set using emplace() and see that inserting duplicate values have not impact.

#include <iostream> 
#include <set> 
#include <string> 
using namespace std; 
  
int main() 
{ 
    set<string> myset{}; 
    myset.emplace("welcome"); 
    myset.emplace("to"); 
    myset.emplace("OpenGenus"); 
    myset.emplace("IQ"); 
    
    myset.emplace("!"); //adding unique element
    myset.emplace("to");  //already present
  
    // printing the set 
    for (auto it = myset.begin(); 
         it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
} 

Output:

Welcome to OpenGenus IQ!

Example 3

In this C++ example, we will insert integer values in a set using emplace() and multiple all integers to get the final product.

#include <iostream> 
#include <set> 
using namespace std; 
  
int main() 
{ 
  
    int mul = 1; 
    set<int> myset{}; 
    myset.emplace(1); 
    myset.emplace(2); 
    myset.emplace(3); 
    myset.emplace(4); 
    myset.emplace(5); 
  
    set<int>::iterator it; 
  
    while (!myset.empty()) 
    { 
        it = myset.begin(); 
        mul = mul*(*it); 
        myset.erase(it); 
    } 
  
    cout << mul; 
    return 0; 
} 

Output :

120

Difference between insert() and emplace()

While using insert one creates an object and then insert it into a set/multiset. But in case of emplace() the object is constructed in-place.

#include <iostream> 
#include <set> 
using namespace std; 
  
int main() 
{ 
  
   // declaring map 
    multiset<pair<int, int>> ms; 
       
    // using emplace() to insert pair in-place 
    ms.emplace(1, 2); 
       
    // Below line would not compile 
    // ms.insert(3, 4);     
       
    // using emplace() to insert pair in-place 
    ms.insert(make_pair(3, 4));     
       
    // printing the multiset 
    for (auto it = ms.begin(); it != ms.end(); ++it) 
        cout << " " << (*it).first << " "
             << (*it).second << endl; 
   
    return 0;
} 

Output :

 1 2
 3 4

Key point

Key points about emplace():

  • Emplace was introduced in C++11

  • Emplace can insert a pair without creating a pair explicitly

  • Emplace is considered to more faster than insert() and advised to be used when your object is non-trival.