- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <unordered_multiset>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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