Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values.
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.
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 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; } |
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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum cost required to convert all Subarrays of size K to a single element
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Difference between cout and std::cout in C++
- C++ program to check whether a String is a Pangram or not
- Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-C++
- Lexicographical smallest number after at most K consecutive swaps
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.

