C++ Set 庫 - set() 函式



描述

C++ 建構函式 std::set::set()(移動建構函式)使用移動語義構造集合容器,其中包含其他集合的內容,即構造一個獲取 x 元素的集合容器。

如果未提供 alloc,則分配器透過從屬於其他分配器的分配器移動構造獲得。

宣告

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

C++11

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

C++14

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

引數

  • alloc - 輸入迭代器到初始位置。

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

返回值

建構函式從不返回值。

異常

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

時間複雜度

常數,即 O(1),除非當前集合的 alloc 與 x 的分配器不同。

示例

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

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // Default constructor
   std::set<char> t_set;
   t_set.insert('x');
   t_set.insert('y');

   std::cout << "Size of set container t_set is : " << t_set.size();

   // Move constructor
   std::set<char> t_set_new(std::move(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
廣告