C++ multiset Library - Assignment Operator(=)



Description

The multiset::assignment operator(=) in C++ STL is used to assign the elements of one multiset to another. While copying the elements, it replaces the current elements of the multiset with the elements of another multiset or initializer list. It copies or moves the elements by maintaining its sorted order.

Syntax

Following is the syntax of multiset assignment operator −

multiset& operator=(const multiset& other);
multiset& operator=(multiset&& other);
multiset& operator=(initializer_list<value_type> ilist);

Return value

The operator returns a reference to the current multiset object (*this).

Exceptions

The operator may throw exceptions if element copy/move operations throw.

Time complexity

The time complexity of multiset operator= is O(n), where n is the number of elements being assigned.

Examples of multiset operator=

The following examples demonstrate the usage of multiset operator=

Assign One Multiset to Another

In this example, we have created a multiset of integers nums1 and used the assignment operator to assign its elements to another multiset nums2.

#include <iostream>
#include <set>
using namespace std;
int main() {
     multiset<int> nums1 = {50, 20, 30, 20, 10, 50, 40};
     multiset<int> nums2;
     
     cout << "Original multiset:";
     for (auto it = nums1.begin(); it != nums1.end(); ++it)
          cout << " " << *it;
     cout << endl;
     
     // Using copy assignment operator
     nums2 = nums1;
     
     cout << "Copied multiset:";
     for (auto it = nums2.begin(); it != nums2.end(); ++it)
          cout << " " << *it;
     cout << endl;
     
     return 0;
}

The output of the above code is given below −

Original multiset: 10 20 20 30 40 50 50
Copied multiset: 10 20 20 30 40 50 50

Assigning Empty Multiset to a Non-empty Multiset

In this example, we have a non-empty multiset nums1. Using the assignment operator, we assign an empty multiset empty to existing non-empty multiset that clears the non-empty multiset making its size 0.

#include <iostream>
#include <set>
using namespace std;

int main() {
    multiset<int> nums1 = {1, 2, 3, 4};
    cout << "Original multiset elements: ";
    for (int x : nums1)
        cout << x << " ";
    cout << endl;
    cout << "Original multiset Size: " << nums1.size();

    // Creating an empty multiset
    multiset<int> empty;
    // Assigning empty multiset to nums1
    nums1 = empty;
    cout << "\nSize after assignment: " << nums1.size();
}
Original multiset elements: 1 2 3 4 
Original multiset Size: 4
Size after assignment: 0

C++ Multiset Assignment Using Initializer List

In this example, we have used the assignment operator with an initializer list to add new values to an existing multiset numbers.

#include <iostream>
#include <set>
using namespace std;
int main() {
    multiset<int> numbers = {5, 15, 25};
    
    cout << "Initial multiset: ";
    for (int x : numbers)
        cout << x << " ";
    cout << endl;
    
    // Assignment using initializer list
    numbers = {10, 20, 30, 20, 10};
    
    cout << "After assignment: ";
    for (int x : numbers)
        cout << x << " ";
    cout << endl;
    
    return 0;
}

The output of the above code is given below −

Initial multiset: 5 15 25
After assignment: 10 10 20 20 30
multiset.htm
Advertisements