C++ Set 庫 - ~set() 函式



描述

C++ 解構函式 std::set::~set() 銷燬 set 容器。這確保了已使用的儲存空間被釋放。

注意:如果元素是指標,則指向的物件不會被銷燬。它僅確保所有迭代器、指標和引用都失效。

宣告

以下是 std::set::~set() 解構函式在不同 C++ 版本中的工作方式。

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.

返回值

解構函式從不返回值。

異常

如果丟擲任何異常,此成員函式無效。

時間複雜度

線性於容器的大小,即 O(N)

示例

以下示例演示了 std::set::~set() 解構函式的使用。

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

以上程式將編譯並正確執行。

當它從 main(); 返回時,解構函式 ~set() 將被呼叫以銷燬 set 容器 't_set'

set.htm
廣告