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)
multiset.htm
Advertisements