The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_multiset to another unordered_multiset and unordered_multiset::operator= is the corresponding operator function. There are three versions of this function:
- The first version takes reference of an unordered_multiset as an argument and copies it to an unordered_multiset. Syntax:
ums1.operator=(unordered_multiset &ums2)
- Parameters: The first version takes the reference of an unordered_multiset as argument.
- The second version performs a move assignment i.e it moves the content of an unordered_multiset to another unordered_multiset. Syntax:
ums1.operator=(unordered_multiset &&ums2)
- Parameters: The second version takes the r-value reference of an unordered_multiset as argument
- The third version assigns contents of an initializer list to an unordered_multiset. Syntax:
ums1.operator=(initializer list)
- Parameters: The third version takes an initializer list as argument.
Return Value: All of them return the value of this pointer(*this). The following program illustrates unordered_multiset::operator= .
CPP
// C++ code to illustrate the method // unordered_multiset::operator=() #include <iostream> #include <unordered_set> using namespace std;
// merge function template < class T>
T merge(T a, T b) { T t(a);
t.insert(b.begin(), b.end());
return t;
} int main()
{ unordered_multiset< int > sample1, sample2, sample3;
// List initialization
sample1 = { 1, 2, 2, 3, 3, 4, 4, 4, 3, 4 };
sample2 = { 1, 2, 3, 1, 4 };
// Merge both unordered_multisets and
// move the result to sample1
sample3 = merge(sample1, sample2);
// copy assignment
sample1 = sample3;
// Print the unordered_set list
for ( auto it = sample1.begin(); it != sample1.end(); ++it)
cout << *it << " " ;
cout << endl;
for ( auto it = sample2.begin(); it != sample2.end(); ++it)
cout << *it << " " ;
cout << endl;
for ( auto it = sample3.begin(); it != sample3.end(); ++it)
cout << *it << " " ;
cout << endl;
} |
Output:
1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 3 2 1 1 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4
Time Complexity-O(N)