當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Set set()用法及代碼示例


描述

C++ 構造函數std::set::set()(Copy Constructor) 用其他容器的內容副本構造集合容器。如果未提供 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- 另一個相同類型的集合容器對象。

返回值

構造函數從不返回任何值。

異常

該成員函數在拋出任何異常時無效。

時間複雜度

與其他的大小呈線性關係;即 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

相關用法


注:本文由純淨天空篩選整理自 C++ Set Library - set() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。