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
广告

© . All rights reserved.