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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// INTEGER MULISET 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;
}

chevron_right


Output:

mymultilist1 = 3 4
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

mymultilist1 = x y
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

mymultilist1 = GeeksForGeeks

Time Complexity: O(n)

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

mylist1 = GeeksForGeeks

Time Complexity: O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.