TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ multiset emplace() function

C++ multiset emplace() Function with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

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

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:

#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:

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:

#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:

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:

#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:

Sum of elements is: 16

Example 4

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

#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:

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.

Next TopicC++ multiset




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf