C++ 集合庫 - set() 函式



描述

C++ 建構函式 std::set::set() 構造一個空的集合容器,其中不包含任何元素。

宣告

以下是來自 std::set 標頭檔案的 std::set::set() 建構函式宣告。

C++98

explicit set(const key_compare& comp = key_compare(),
             const allocator_type& alloc = allocator_type());

C++11

explicit set(const key_compare& comp = key_compare(),
             const allocator_type& alloc = allocator_type());
explicit set(const allocator_type& alloc);

C++14

set();
explicit set(const key_compare& comp,
             const allocator_type& alloc = allocator_type());
explicit set(const allocator_type& alloc);

引數

  • alloc − 輸入迭代器指向初始位置。

  • comp − 用於所有鍵比較的比較函式物件。

返回值

建構函式不返回任何值。

異常

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

時間複雜度

常數,即 O(1)

示例

以下示例演示了 std::set::set() 建構函式的用法。

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // Default constructor
   std::set<char> t_set;
  
   t_set.insert('a');
   t_set.insert('e');
   t_set.insert('i');
   t_set.insert('o');
   t_set.insert('u');
  
   int size = t_set.size(); 

   std::cout << "Contents of set container t_set = " << size;
   return 0;
}

讓我們編譯並執行上述程式,這將產生以下結果:

Contents of set container t_set = 5
set.htm
廣告
© . All rights reserved.