- 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 - Copy Constructor
Description
The multiset::multiset() copy constructor in C++ STL is used to create a new multiset by copying all elements from another multiset. The order of elements remains same as the original multiset while copying.
The range constructor creates a new multiset from any container, like arrays, multisets, or vectors, whereas the copy constructor creates a new multiset by copying elements only from an existing multiset.
Syntax
Following is the syntax of multiset copy constructor −
multiset(const multiset& other); // C++98 multiset(const multiset& other, const Allocator& alloc); // C++11
Return value
The copy constructor does not return any value.
Exceptions
It may throw exceptions if element construction, comparison, or memory allocation fails.
Time complexity
The time complexity of multiset copy constructor is O(N), where N is the number of elements in the source multiset.
Examples of multiset Copy Constructor
The following examples demonstrate the usage of multiset copy constructor −
Creating a Copy of Integer Multiset
In this example, we have created a multiset of integers and then used the copy constructor to create a duplicate multiset with the same elements.
#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> copy(original);
cout << "\nCopied multiset size: " << copy.size() << endl;
cout << "Copied multiset elements:";
printMs(copy);
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 Copied multiset size: 7 Copied multiset elements: 10 20 20 30 40 50 50
Copying a String Multiset
Here is an example of using the copy constructor to create a duplicate 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 elements:";
for (const auto &fruit : fruits)
cout << " " << fruit;
cout << endl;
multiset<string> fruitsCopy(fruits);
cout << "Copied multiset elements:";
for (const auto &fruit : fruitsCopy)
cout << " " << fruit;
cout << endl;
return 0;
}
The output of the above code is given below −
Original multiset elements: apple apple apple banana banana cherry Copied multiset elements: apple apple apple banana banana cherry
Using Copy Constructor with Custom Comparator
The following example demonstrates copying 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 copied 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: ";
for (int x : nums)
cout << x << " ";
cout << endl;
multiset<int, greater<int>> copied(nums);
cout << "\nCopied multiset: ";
for (int x : copied)
cout << x << " ";
cout << endl;
}
The output of the above code is given below −
Original multiset: 30 25 25 15 15 10 Copied multiset: 30 25 25 15 15 10
Copying a Multiset of Custom Objects
Here is an example of copy constructor to copy 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 elements:\n";
for (const Point &p : points1){
cout << "(" << p.x << ", " << p.y << ") ";
}
cout << endl;
multiset<Point, ComparePoint> points2(points1); // Copy constructor
cout << "Multiset elements after copying:\n";
for (const Point &p : points2){
cout << "(" << p.x << ", " << p.y << ") ";
}
}
The output of the above code is given below −
Original multiset elements: (0, 0) (1, 2) (1, 5) Multiset elements after copying: (0, 0) (1, 2) (1, 5)