- 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 - 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.