Multiset Emplace Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Multiset Emplace Function

Multiset Emplace Function

BLUF: Mastering Multiset Emplace Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Multiset Emplace Function

C++ is renowned for its efficiency. Learn how Multiset Emplace Function enables low-level control and high-performance computing in the tutorial below.

C++ multiset emplace

The emplace function in C++ for multiset is employed to enlarge the multiset container by adding fresh elements directly into the container without copying or moving them.

The element's constructor is invoked by providing the arguments passed to this function.

Syntax

Example

template <class? Args>
    iterator emplace (Args&&? args);    //since C++ 11

Parameter

The parameters passed to create an element for insertion into the container.

Return value

The emplace method provides a boolean pair to signal whether the insertion took place and yields an iterator pointing to the newly added element.

Complexity

Logarithmic in the container size.

Iterator validity

No changes.

Data Races

The container is modified.

Iterating over ranges within the container poses a safety risk, whereas simultaneous access to existing elements remains secure.

Exception Safety

If an error is raised, the multiset container remains unchanged.

Example 1

Let's explore a straightforward example of adding elements to the multiset:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   
   multiset<char> m;

   m.emplace('a');
   m.emplace('b');
   m.emplace('a');
   m.emplace('c');
   m.emplace('b');

   cout << "Multiset contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << *it<< ", ";

   return 0;
}

Output:

Output

Multiset contains following elements
a, a, b, b, c,

In the aforementioned example, it merely adds the element to the multiset m using the provided values.

Example 2

Let's examine a basic example to insert the element and validate the presence of a duplicate key:

Example

#include <set>  
#include <string>  
#include <iostream>  
  
using namespace std;  
  
template <typename S> void print(const S& s) {  
    cout << s.size() << " elements: ";  
  
    for (const auto& p : s) {  
        cout << "(" << p << ") ";  
    }  
  
    cout << endl;  
}  
  
int main()  
{  
    multiset<string> s1;  
  
    s1.emplace("Deep");  
    s1.emplace("Nikita");  
    s1.emplace("Kesharwani");  
  
    cout << "multiset modified, now contains ";  
    print(s1);  
    cout << endl;  
  
    s1.emplace("Nikita");  
  
    cout << "multiset modified, now contains ";  
    print(s1);  
    cout << endl;  
}

Output:

Output

multiset modified, now contains 3 elements: (Deep) (Kesharwani) (Nikita) 

multiset modified, now contains 4 elements: (Deep) (Kesharwani) (Nikita) (Nikita)

Example 3

Let's explore a basic example to calculate the total of the provided values:

Example

#include <iostream>
#include <set>
using namespace std;
 
int main()
{
    // sum variable declaration
    int sum = 0;
 
    // multiset declaration
    multiset<int> mymultiset{};

    mymultiset.emplace(1);
    mymultiset.emplace(3);
    mymultiset.emplace(4);
    mymultiset.emplace(1);
    mymultiset.emplace(2);
    mymultiset.emplace(2);
    mymultiset.emplace(3);
 
    // iterator declaration
    multiset<int>::iterator it;
 
    // finding sum of elements
    while (!mymultiset.empty()) {
        it = mymultiset.begin();
        sum = sum + *it;
        mymultiset.erase(it);
    }
 
    // printing the sum
    cout << "Sum of elements is: "<<sum;
    return 0;
}

Output:

Output

Sum of elements is: 16

Example 4

Let's see a simple example to insert the element:

Example

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

int main() {

  typedef multiset<string> city;  
   string name;
   city fmly ;
   int n;

   cout<<"Enter the number of family members :";
   cin>>n;

   cout<<"Enter the name of each member: \n";
   for(int i =0; i<n; i++)
   {
       cin>> name;      // Get key
       fmly.emplace(name);
       
   }
   
      cout<<"\nTotal member of family is:"<< fmly.size();

      cout<<"\nDetails of family members: \n";
      cout<<"\nName \n ________________________\n";
      city::iterator p;
      for(p = fmly.begin(); p!=fmly.end(); p++)
      {
          cout<<(*p)<<" \n ";
      }
    
   return 0;
}

Output:

Output

Enter the number of family members: 3
Enter the name of each member: 
Bob
Robin
David

Total member of family is: 3
Details of family members: 

Name 
 ________________________
Bob 
David 
Robin

In the example provided, it merely inserts the elements based on the user's selection.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience