C++ Set Library - ~set() Function


Advertisements

Description

The C++ destructor std::set::~set() destructs the set container. This ensures that the used storage is deallocated.

Note: If the elements are pointers, the pointed-to objects are not destroyed. It only ensures all iterators, pointers and references are invalidated.

Declaration

Following are the ways in which std::set::~set() destructor works in various C++ versions.

C++98

~set() destroys all set container elements, and deallocates all the 
storage capacity allocated by the container using its allocator.

C++11

~set() calls allocator_traits::destroy on each of the contained 
elements, and deallocates all the storage capacity allocated by the
 set container using its allocator.

C++14

~set() calls allocator_traits::destroy on each of the contained 
elements, and deallocates all the storage capacity allocated by the
 set container using its allocator.

Return value

Destructor never returns any value.

Exceptions

This member function has no effect in case any exception is thrown.

Time complexity

Linear in size of the container, i.e. O(N)

Example

The following example shows the usage of std::set::~set() destructor.

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main(void) {
   //Default constructor
   std::set<string> t_set;

   t_set.insert("Tutorials Point");
   return 0;
}

The above program will compile and execute properly.

The moment it returns from main(); destructor ~set() will be called to destroy the set container 't_set'

set.htm
Advertisements