TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset operator= function

C++ multiset operator= 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 operator=

There are following three uses of operator= in multiset:

  1. operator= is used to assign new content to the multiset container by replacing its old content (or copy the content) and modifies size if necessary.
  2. operator= is used to move the content of one multiset container into another and modifies size if necessary.
  3. operator= is used to copy the elements from initializer list to multiset container.

Syntax

copy(1)      multiset& operator= (const multiset& x);                                //until C++ 11

copy (1)       multiset& operator= (const multiset& x);		                 //since C++ 11

move (2)        multiset& operator= (multiset&& x);                                   //since C++ 11

initializer list (3)	multiset& operator= (initializer_list il);      //since C++ 11

copy (1):- Copies all the elements from x into the multiset container.

move (2):- Moves the contents of x into the multiset container.

initializer_list (3):- Copies the elements of il into the multiset container.

Parameter

x: A multiset object with the same type.

il: An initializer list object.

Return value

this pointer.

Complexity

Copy assignment: Linear in sizes.

Move assignment: Linear in current container size.

Initializer list assignment: Up to logarithmic in sizes.

Iterator validity

All references, iterators and pointers related to this multiset container are invalidated.

Data Races

All copied elements are accessed.

The move assignment modifies x.

The multiset container and all its elements are modified.

Exception Safety

If an exception is thrown, the container is in a valid state.

Example 1

Let's see the simple example to copy the content of one multiset to another:

#include <iostream>
#include <set>

using namespace std;

int main(void) {

   multiset<int> s1 = {10,20,10,30};

   cout << "Multiset s1 contains following elements" << endl;
    for (auto it = s1.begin(); it != s1.end(); ++it)
      cout << *it << endl;
      
    multiset<int> s2 = s1;  
    cout<<"\nAfter copying the elements from s1 to s2... \n";  
    
    cout << "\nMultiset s2 contains following elements" << endl;
    for (auto it = s2.begin(); it != s2.end(); ++it)
      cout << *it<< endl; 

   return 0;
}

Output:

Multiset s1 contains following elements
10
10
20
30

After copying the elements from s1 to s2... 

Multiset s2 contains following elements
10
10
20
30

In the above example, operator = is used to copy the content of one multiset s1 to another multiset s2.

Example 2

Let's see a simple example to move the elements of one multiset to another:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   
   multiset<char> s1 = {'a','e','i','o','u','e','u'};

      cout << "Multiset m1 contains following elements" << endl;
    for (auto it = s1.begin(); it != s1.end(); ++it)
      cout << *it << ", ";
      
    multiset<char> s2 = move(s1); 
    cout<<"\n\nAfter moving the elements from s1 to s2... \n";  
    
    cout << "\nMultiset s2 contains following elements" << endl;
    for (auto it = s2.begin(); it != s2.end(); ++it)
      cout << *it << ", ";

   return 0;
}

Output:

Multiset m1 contains following elements
a, e, e, i, o, u, u, 

After moving the elements from s1 to s2... 

Multiset s2 contains following elements
a, e, e, i, o, u, u,

In the above example, operator = is used to move the content of one multiset s1 to another multiset s2.

Example 3

Let's see a simple example to copy the content from initializer list to multiset:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<int> s;

   s = {100, 200, 300, 100, 300};  //initializer list

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

   for (auto it = s.begin(); it != s.end(); ++it)
      cout << *it << endl;

   return 0;
}

Output:

Multiset contains the following elements
100
100
200
300
300

In the above example, operator = is used to copy the content from initializer list to multiset m.

Example 4

Let's see a simple example:

#include <iostream>
#include <set>

using namespace std;

int  main () 
{ 
  int  values []  =  {  5 ,  2 ,  4 ,  1 ,  0 ,  0 ,  9  }; 
  multiset < int >  c1 ( values ,  values  +  7 ); 
  multiset < int >  c2 ;

  c2  =  c1 ; 
  c1  =  multiset < int > ();

  cout<<  "Size Of c1:"  <<  c1 . size ()  << endl ; 
  cout<< "Size Of c2:"  <<  c2 . size ()  << endl ; 
}

Output:

Size Of c1:0
Size Of c2:7

In the above example, there are two multisets c1 and c2. c1 has 7 elements and c2 is empty, but after assigning c1 to c2, size of c1 become 0 and size of c2 become 7.

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