multiset::emplace() in C++ STL

Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values.

multiset::emplace()

This function is used to insert a new element into the multiset container.

Syntax :



multisetname.emplace(value)
Parameters :
The element to be inserted into the multiset
is passed as the parameter.
Result :
The parameter is added to the multiset.

Examples:

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

Input  : mymultiset{};
         mymultiset.emplace("This");
         mymultiset.emplace("is");
         mymultiset.emplace("Geeksforgeeks");
Output : mymultiset = This, is, Geeksforgeeks 

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 same type as that of the container, otherwise an error is thrown

filter_none

edit
close

play_arrow

link
brightness_4
code

// INTEGER EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <set>
using namespace std;
  
int main()
{
    multiset<int> mymultiset{};
    mymultiset.emplace(1);
    mymultiset.emplace(56);
    mymultiset.emplace(4);
    mymultiset.emplace(9);
    mymultiset.emplace(0);
    // multi set becomes 1, 56, 4, 9, 0
  
    // adding another element
    mymultiset.emplace(87);
  
    // printing the multiset
    for (auto it = mymultiset.begin();
         it != mymultiset.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

1 56 4 9 0 87
filter_none

edit
close

play_arrow

link
brightness_4
code

// STRING EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <set>
#include <string>
using namespace std;
  
int main()
{
    multiset<string> mymultiset{};
    mymultiset.emplace("This");
    mymultiset.emplace("is");
    mymultiset.emplace("a");
    mymultiset.emplace("computer science");
    mymultiset.emplace("portal");
    // multi set becomes This, a,
    // computer science, is, portal
  
    // adding element
    mymultiset.emplace("GeeksForGeeks");
  
    // printing the multiset
    for (auto it = mymultiset.begin();
         it != mymultiset.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

GeeksForGeeks This a computer science is portal

Application
Input an empty multi set with the following numbers and order using emplace() function and find sum of elements. The advantage of emplace() over insert is, it avoids unnecessary copy of object.

Input :  7, 9, 4, 6, 2, 5, 3
Output : 36
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

36

Time Complexity : O(logn)

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

 a 24
 b 25

Please refer Inserting elements in std::map (insert, emplace and operator []) for details.



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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.