C++ multiset Library - Destructor



Description

The multiset::~multiset() destructor in C++ STL is used to destroy the multiset object. It deallocates and free up memory used by the container. The destructor is automatically called when the multiset goes out of scope or is deleted.

Syntax

Following is the syntax of multiset destructor −

~multiset();

Return value

The destructor does not return any value.

Exceptions

The destructor does not throw exceptions.

Time complexity

The time complexity of multiset destructor is O(n), where n is the number of elements in the multiset.

Examples of multiset Destructor

The following examples demonstrate the usage of multiset destructor:

Automatic Destruction of Integer Multiset

In this example, we have created a multiset of integers inside a block scope. The destructor is automatically called when the multiset goes out of scope.

#include <iostream>
#include <set>
using namespace std;
int main() {
     {
          multiset<int> numbers = {50, 20, 30, 20, 10, 50, 40};
          cout << "Multiset elements:";
          for (auto it = numbers.begin(); it != numbers.end(); ++it)
               cout << " " << *it;
          cout << endl;
          cout << "\nMultiset going out of scope..." << endl;
     }
     // Destructor called here automatically
     cout << "Multiset has been destroyed." << endl;

     return 0;
}

The output of the above code is given below −

Multiset elements: 10 20 20 30 40 50 50

Multiset going out of scope...
Multiset has been destroyed.

Destructor with String Multiset

Here is an example demonstrating the automatic destruction of a string multiset when it goes out of scope −

#include <iostream>
#include <set>
using namespace std;
int main() {
    cout << "Creating string multiset..." << endl; {
        multiset<string> fruits = {"apple", "banana", "apple", "cherry", "banana", "apple"};
        cout << "Multiset size: " << fruits.size() << endl;
        cout << "Multiset elements:";
        for (const auto &fruit : fruits)
            cout << " " << fruit;
        cout << endl;
    } 
    // Destructor automatically called here
    
    cout << "String multiset has been destroyed." << endl;
    
    return 0;
}

The output of the above code is given below −

Creating string multiset...
Multiset size: 6
Multiset elements: apple apple apple banana banana cherry
String multiset has been destroyed.

C++ multiset Destructor Using delete

In this example, first, we dynamically allocate a multiset of integers using new and then call the destructor using delete to free up the memory.

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

int main() {
    multiset<int> *ms = new multiset<int>({10, 20, 30});

    cout << "Multiset elements: ";
    for (int x : *ms)
        cout << x << " ";
    cout << endl;

    delete ms; // destructor called here
    cout << "Destructor called using delete\n";

    return 0;
}

The output of the above code is given below −

Multiset elements: 10 20 30
Destructor called using delete

Destructor with Custom Objects

Here is an example demonstrating the destructor cleaning up a multiset of custom objects. The destructor of each Point object is also called while freeing up the space −

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

struct Point {
     int x, y;

     Point(int a, int b) : x(a), y(b) {
          cout << "Point(" << x << ", " << y << ") constructed" << endl;
     }

     ~Point() {
          cout << "Point(" << x << ", " << y << ") destroyed" << endl;
     }
};

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;
     }
};

int main() {
     {
          multiset<Point, ComparePoint> points;
          points.emplace(1, 2);
          points.emplace(1, 5);
          points.emplace(0, 0);

          cout << "Multiset elements:\n";
          for (const Point &p : points)
          {
               cout << "(" << p.x << ", " << p.y << ") ";
          }
          cout << endl;

          cout << "\nMultiset going out of scope..." << endl;
     }

     cout << "\nMultiset has been destroyed." << endl;
     return 0;
}

The output of the above code is given below −

Point(1, 2) constructed
Point(1, 5) constructed
Point(0, 0) constructed
Multiset elements:
(0, 0) (1, 2) (1, 5) 

Multiset going out of scope...
Point(1, 5) destroyed
Point(1, 2) destroyed
Point(0, 0) destroyed

Multiset has been destroyed.
multiset.htm
Advertisements