Related Articles

# set::emplace() in C++ STL

• Difficulty Level : Basic
• Last Updated : 23 Jan, 2018

Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.

set::emplace()

This function is used to insert a new element into the set container, only if the element to be inserted is unique and does not already exists in the set.

Syntax :

```setname.emplace(value)
Parameters :
The element to be inserted into the set
is passed as the parameter.
Result :
The parameter is added to the set if
the set does not contain that element already.
```

Examples:

```Input  : myset{1, 2, 3, 4, 5};
myset.emplace(6);
Output : myset = 1, 2, 3, 4, 5, 6

Input  : myset{1, 2, 3, 4, 5};
myset.emplace(4);
Output : myset = 1, 2, 3, 4, 5
```

Errors and Exceptions
1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown
2. Parameter should be of the same type as that of the container otherwise, an error is thrown

 `// INTEGER SET EXAMPLE``// CPP program to illustrate``// Implementation of emplace() function``#include ``#include ``using` `namespace` `std;`` ` `int` `main()``{``    ``set<``int``> myset{};``    ``myset.emplace(2);``    ``myset.emplace(6);``    ``myset.emplace(8);``    ``myset.emplace(9);``    ``myset.emplace(0);``    ``// set becomes 0, 2, 6, 8, 9`` ` `    ``// adding unique element`` ` `    ``myset.emplace(5);``    ``// set becomes 0, 2, 5, 6, 8, 9`` ` `    ``// adding element which already``    ``// exists there will be no``    ``// change in the set`` ` `    ``myset.emplace(2);``    ``// set remains 0, 2, 5, 6, 8, 9`` ` `    ``// printing the set``    ``for` `(``auto` `it = myset.begin();``         ``it != myset.end(); ++it)``        ``cout << ``' '` `<< *it;``    ``return` `0;``}`

Output:

```0 2 5 6 8 9
```
 `// STRING SET EXAMPLE``// CPP program to illustrate``// Implementation of emplace() function``#include ``#include ``#include ``using` `namespace` `std;`` ` `int` `main()``{``    ``set myset{};``    ``myset.emplace(``"This"``);``    ``myset.emplace(``"is"``);``    ``myset.emplace(``"a"``);``    ``myset.emplace(``"computer science"``);``    ``myset.emplace(``"portal"``);``    ``// set becomes This, a, computer``    ``// science, is, portal`` ` `    ``// adding unique element``    ``myset.emplace(``"GeeksForGeeks"``);``    ``// set becomes GeeksForGeeks, This, is,``    ``// a, computer science, portal`` ` `    ``// adding element which already exists``    ``// there will be no change in the set``    ``myset.emplace(``"is"``);`` ` `    ``// set remains GeeksForGeeks, This, is,``    ``// a, computer science, portal`` ` `    ``// printing the set``    ``for` `(``auto` `it = myset.begin();``         ``it != myset.end(); ++it)``        ``cout << ``' '` `<< *it;``    ``return` `0;``}`

Output:

```GeeksForGeeks This a computer science is portal
```

Time Complexity : O(logn)

Application
Input an empty multi set with the following numbers and order using emplace() function and find sum of elements.

```Input :  7, 9, 4, 6, 2, 5, 3
Output : 36```
 `// CPP program to illustrate``// Application of emplace() function``#include ``#include ``using` `namespace` `std;`` ` `int` `main()``{``    ``// sum variable declaration``    ``int` `sum = 0;`` ` `    ``// set declaration``    ``set<``int``> myset{};``    ``myset.emplace(7);``    ``myset.emplace(9);``    ``myset.emplace(4);``    ``myset.emplace(6);``    ``myset.emplace(2);``    ``myset.emplace(5);``    ``myset.emplace(3);`` ` `    ``// iterator declaration``    ``set<``int``>::iterator it;`` ` `    ``// finding sum of elements``    ``while` `(!myset.empty()) {``        ``it = myset.begin();``        ``sum = sum + *it;``        ``myset.erase(it);``    ``}`` ` `    ``// printing the sum``    ``cout << sum;``    ``return` `0;``}`

Output :

```36
```

emplace() vs insert
When we use insert, we create an object and then insert it into the multiset. With emplace(), the object is constructed in-place.

 `// C++ code to demonstrate difference between``// emplace and insert``#include``using` `namespace` `std;`` ` `int` `main()``{``    ``// declaring set``    ``set> ms;``     ` `    ``// using emplace() to insert pair in-place``    ``ms.emplace(``'a'``, 24);``     ` `    ``// Below line would not compile``    ``// ms.insert('b', 25);    ``     ` `    ``// using emplace() to insert pair in-place``    ``ms.insert(make_pair(``'b'``, 25));    ``     ` `    ``// printing the set``    ``for` `(``auto` `it = ms.begin(); it != ms.end(); ++it)``        ``cout << ``" "` `<< (*it).first << ``" "` `             ``<< (*it).second << endl;`` ` `    ``return` `0;``}`

Output :

``` a 24
b 25
```
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