C++ Unordered_set::emplace() Function



TheC++ std::unordered_set::emplace() function is used to insert a new element in the unordered_set if its value is unique. The insertion will only takes place if the element does not exists in the unordered_set. when this function is invoked, it insert a new element avoiding the unneccasry copy or move operations.

Syntax

Following is the syntax of std::unordered_set::emplace() function.

pair <iterator,bool> emplace ( Args&&... args );

Parameters

  • args − It indicates the arguments passed to the constructor.

Return Value

This function returns an element and a boolean value pair. If the function successfully inserts the element, it returns a pair with an iterator for the newly inserted element and a value of true. If the element to be inserted is already present in the unordered_set, then it returns a pair of iterators pointing to the already-present element and a boolean value of false.

Example 1

Let's look at the following example, where we are going to demonstrate the usage of unordered_set::emplace() function.

#include <iostream>
#include <string>
#include <unordered_set>

int main () {
   std::unordered_set<std::string> myset;

   myset.emplace ("kittu");
   myset.emplace ("prasad");
   myset.emplace ("sairamkrishna");

   std::cout << "myset containing:";
   for (const std::string& x: myset) std::cout << " " << x;

   std::cout << std::endl;
   return 0;
}

Output

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

myset containing: sairamkrishna prasad kittu

Example 2

Consider the following example, where we are going to use the emplace() function and inserting the element that is not present in the unordered_set.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<int> myUset={10, 20};

   myUset.emplace (30);
   myUset.emplace (40);
   myUset.emplace (50);

   cout << "myUset containing:";
   for (const int& x: myUset)
      cout << " " << x;
   cout << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

myUset containing: 50 40 30 20 10

Example 3

In the following example, we are going to insert the element which was already present in the unordered_set using the emplace() function and observing the output.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<int> myUset={10, 20, 30, 40};

   myUset.emplace (30);
   myUset.emplace (40);

   cout << "myUset containing:";
   for (const int& x: myUset)
      cout << " " << x;
   cout << endl;
   return 0;
}

Output

Following is the output of the above code −

myUset containing: 40 30 20 10

Example 4

Following is the example, where we are going to use the emplace() function and inserting the element into the unordered_set, if the element exists in the unordered_set then it will doesn't make any changes.

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
   unordered_set<int> myUset{10, 20, 30};
 
   auto Insert = myUset.emplace(40);
   if(!Insert.second) 
      cout<<"40 is already present in myUset.\n";
   else
      cout<<"40 is added in myUset.\n";

   Insert = myUset.emplace(20);
   if(!Insert.second) 
      cout<<"20 is already present in myUset.\n";
   else
      cout<<"20 is added in myUset.\n";

   return 0;
}

Output

Output of the above code is as follows −

40 is added in myUset.
20 is already present in myUset.
Advertisements