Open In App

multiset::operator= in C++ STL

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

multiset::operator=

This operator is used to assign new contents to the container by replacing the existing contents. 
It also modifies the size according to the new contents.
Syntax : 
 

multisetname1 = (multisetname2)
Parameters :
Another container of the same type.
Result :
Assign the contents of the container passed as 
parameter to the container written on left side of the operator.

 

set::operator=

This operator is used to assign new contents to the container by replacing the existing contents. 
It also modifies the size according to the new contents.
Syntax : 
 

setname1 = (setname2)
Parameters :
Another container of the same type.
Result :
Assign the contents of the container passed as 
parameter to the container written on left side of the operator.

Examples: 
 

Input  :  mymultiset1 = 1, 2, 3
          mymultiset2 = 3, 2, 1, 4
          mymultiset1 = mymultiset2;
Output :  mymultiset1 = 3, 2, 1, 4

Input  :  mymultiset1 = 2, 6, 1, 5
          mymultiset2 = 3, 2
          mymultiset1 = mymultiset2;
Output :  mymultiset1 = 3, 2

 

Input  :  myset1 = 1, 2, 3
          myset2 = 3, 2, 1, 4
          myset1 = myset2;
Output :  myset1 = 3, 2, 1, 4

Input  :  myset1 = 2, 6, 1, 5
          myset2 = 3, 2
          myset1 = myset2;
Output :  myset1 = 3, 2

Errors and Exceptions
1. If the containers are of different types, an error is thrown. 
2. It has a basic no exception throw guarantee otherwise.
 




// INTEGER MULTISET EXAMPLE
// CPP program to illustrate
// Implementation of = operator
#include <iostream>
#include <set>
using namespace std;
  
int main()
{
    multiset<int> mymultiset1{ 1, 7, 4, 9, 0};
    multiset<int> mymultiset2{ 3, 4 };
    mymultiset1 = mymultiset2;
    cout << "mymultiset1 = ";
    for (auto it = mymultiset1.begin();
              it != mymultiset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 
 

mymultilist1 = 3 4

 




// CHARACTER MULTISET EXAMPLE
// CPP program to illustrate
// Implementation of = operator
#include <iostream>
#include <set>
using namespace std;
  
int main()
{
    multiset<char> mymultiset1{ 'a', 'b', 'c'};
    multiset<char> mymultiset2{ 'x', 'y' };
    mymultiset1 = mymultiset2;
    cout << "mymultiset1 = ";
    for (auto it = mymultiset1.begin();
              it != mymultiset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 
 

mymultilist1 = x y

 




// STRING MULTISET EXAMPLE
// CPP program to illustrate
// Implementation of = operator
#include <iostream>
#include <set>
#include<string>
using namespace std;
  
int main()
{
    multiset<string> mymultiset1{ "This","is","a","computer science portal"};
    multiset<string> mymultiset2{ "GeeksForGeeks" };
    mymultiset1 = mymultiset2;
    cout << "mymultiset1 = ";
    for (auto it = mymultiset1.begin();
              it != mymultiset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 
 

mymultilist1 = GeeksForGeeks

Time Complexity: O(n)
 




// CPP program to illustrate
// Implementation of = operator
#include <iostream>
#include <set>
#include<string>
using namespace std;
  
int main()
{
    set<string> myset1{ "This","is","a","computer science portal"};
    set<string> myset2{ "GeeksForGeeks" };
    myset1 = myset2;
    cout << "myset1 = ";
    for (auto it = myset1.begin();
              it != myset1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 
 

mylist1 = GeeksForGeeks

Time Complexity: O(n)
 


Article Tags :
C++