C++ multiset Library - Move Constructor



Description

The multiset::multiset() move constructor in C++ STL creates a new multiset using move semantics. It transfers the ownership of elements from one multiset to another multiset (i.e., move elements from one multiset to other multiset). You can use move constructor when you don't need the source multiset.

Syntax

Following is the syntax of multiset move constructor −

multiset(multiset&& other);  // C++11
multiset(multiset&& other, const Allocator& alloc);  // C++11

Return value

The move constructor does not return any value.

Exceptions

It may throw exceptions if memory allocation fails.

Time complexity

The time complexity of multiset move constructor is O(1).

Examples of multiset Move Constructor

The following examples demonstrate the usage of multiset move constructor

Moving an Integer Multiset

In this example, we have created a multiset of integers and then used the move constructor to transfer all elements to a new multiset. After the move operation, the original multiset becomes empty.

#include <iostream>
#include <set>
using namespace std;
void printMs(const multiset<int> &numbers) {
     for (auto it = numbers.begin(); it != numbers.end(); ++it)
          cout << " " << *it;
     cout << endl;
}
int main() {
     multiset<int> original = {50, 20, 30, 20, 10, 50, 40};
     cout << "Original multiset size: " << original.size() << endl;
     cout << "Original multiset elements:";
     printMs(original);
     multiset<int> moved(move(original));
     cout << "\nMoved multiset size: " << moved.size() << endl;
     cout << "Moved multiset elements:";
     printMs(moved);
     cout << "\nOriginal multiset size after move: " << original.size() << endl;
     cout << "Original multiset elements after move:";
     printMs(original);
     
     return 0;
}

The output of the above code is given below −

Original multiset size: 7
Original multiset elements: 10 20 20 30 40 50 50

Moved multiset size: 7
Moved multiset elements: 10 20 20 30 40 50 50

Original multiset size after move: 0
Original multiset elements after move:

Moving a String Multiset

Here is an example of using the move constructor to transfer ownership of a string multiset −

#include <iostream>
#include <set>
using namespace std;
int main() {
    multiset<string> fruits = {"apple", "banana", "apple", "cherry", "banana", "apple"};
    cout << "Original multiset size: " << fruits.size() << endl;
    cout << "Original multiset elements:";
    for (const auto &fruit : fruits)
        cout << " " << fruit;
    cout << endl;
    multiset<string> fruitsMoved(move(fruits));
    cout << "\nMoved multiset size: " << fruitsMoved.size() << endl;
    cout << "Moved multiset elements:";
    for (const auto &fruit : fruitsMoved)
        cout << " " << fruit;
    cout << endl;
    cout << "\nOriginal multiset size after move: " << fruits.size() << endl;
    return 0;
}

The output of the above code is given below −

Original multiset size: 6
Original multiset elements: apple apple apple banana banana cherry

Moved multiset size: 6
Moved multiset elements: apple apple apple banana banana cherry

Original multiset size after move: 0

Using Move Constructor with Custom Comparator

The following example demonstrates moving a multiset that uses a custom comparison function to sort elements in descending order. In the output, it can be seen that the comparator function is also transferred along with the multiset elements −

#include <iostream>
#include <set>
#include <functional>
using namespace std;
int main() {
     multiset<int, greater<int>> nums = {15, 25, 10, 25, 30, 15};
     cout << "Original multiset size: " << nums.size() << endl;
     cout << "Original multiset: ";
     for (int x : nums)
          cout << x << " ";
     cout << endl;
     
     multiset<int, greater<int>> moved(move(nums));
     cout << "\nMoved multiset size: " << moved.size() << endl;
     cout << "Moved multiset: ";
     for (int x : moved)
          cout << x << " ";
     cout << endl;
     
     cout << "\nOriginal multiset size after move: " << nums.size() << endl;
}

The output of the above code is given below −

Original multiset size: 6
Original multiset: 30 25 25 15 15 10 

Moved multiset size: 6
Moved multiset: 30 25 25 15 15 10 

Original multiset size after move: 0

Moving a Multiset of Custom Objects

Here is an example of move constructor to transfer objects from one multiset points1 to another multiset points2

#include <iostream>
#include <set>
using namespace std;
// A simple structure to store a point
struct Point {
     int x, y;
};
struct ComparePoint {
     bool operator()(const Point &a, const Point &b) const {
          if (a.x < b.x)
               return true;
          if (a.x > b.x)
               return false;
          return a.y < b.y; // Compare y if x is same
     }
};
int main() {
     multiset<Point, ComparePoint> points1 = {{1, 2}, {1, 5}, {0, 0}};
     cout << "Original multiset size: " << points1.size() << endl;
     cout << "Original multiset elements:\n";
     for (const Point &p : points1){
          cout << "(" << p.x << ", " << p.y << ") ";
     }
     cout << endl;
     multiset<Point, ComparePoint> points2(move(points1)); // Move constructor
     cout << "\nMoved multiset size: " << points2.size() << endl;
     cout << "Multiset elements after moving:\n";
     for (const Point &p : points2){
          cout << "(" << p.x << ", " << p.y << ") ";
     }
     cout << endl;
     cout << "\nOriginal multiset size after move: " << points1.size() << endl;
}

The output of the above code is given below −

Original multiset size: 3
Original multiset elements:
(0, 0) (1, 2) (1, 5) 

Moved multiset size: 3
Multiset elements after moving:
(0, 0) (1, 2) (1, 5) 

Original multiset size after move: 0
multiset.htm
Advertisements