Multiset Emplace Function

C++ multiset emplace

C++ Multiset emplace function is used to extend the multiset container by inserting new elements into the container. Elements are built directly (neither copied nor moved).

The constructor of the element is called by giving the arguments args passed to this function.

Syntax

Example

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

Parameter

args : The arguments forwarded to construct an element to be inserted into the container.

Return value

emplace function returns a bool pair that will indicate if the insertion is occurred or not, and returns an iterator pointing to the newly inserted element.

Complexity

Logarithmic in the container size.

Iterator validity

No changes.

Data Races

The container is modified.

Iterating ranges in the container is not safe although concurrently accessing exiting elements is safe.

Exception Safety

If an exception is thrown, there are no changes in the multiset container.

Example 1

Let's see the simple example to insert the elements into 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 above example, it simply inserts the element into the multiset m with the given values.

Example 2

Let's see a simple example to insert the element and check for the 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 see a simple example to find the sum of the inserted elements:

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 above example, it simply inserts the elements by the user's choice.

Input Required

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