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



描述

C++建構函式std::set::set()(複製建構函式)使用另一個容器的內容副本構造集合容器。如果未提供alloc,則透過呼叫獲得分配器。

宣告

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

C++98

set (const set& x);

C++11

set (const set& x);
set (const set& x, const allocator_type& alloc);

C++14

set (const set& x);
set (const set& x, const allocator_type& alloc);

引數

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

  • x − 另一個相同型別的集合容器物件。

返回值

建構函式不返回值。

異常

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

時間複雜度

線性於other的大小;即O(n)

示例

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

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   //Default Constructor
   std::set<int> t_set;
   t_set.insert(5);
   t_set.insert(10);

   std::cout << "Size of set container t_set is : " << t_set.size();
  
   // Copy constructor
   std::set<int> t_set_new(t_set);
   std::cout << "\nSize of new set container t_set_new is : " << t_set_new.size();
   return 0;
}

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

Size of set container t_set is : 2
Size of new set container t_set_new is : 2
set.htm
廣告