移动构造函数(Move constructor)
描述 (Description)
C ++构造函数std::set::set() (移动构造函数)使用移动语义构造具有其他集合内容的集合容器,即构造获取x元素的集合容器。
如果未提供alloc,则通过从属于其他的分配器的move-construction获得分配器。
声明 (Declaration)
以下是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);
参数 (Parameters)
alloc - 将迭代器输入到初始位置。
x - 相同类型的另一个set容器对象。
返回值
构造函数永远不会返回任何值。
异常 (Exceptions)
如果抛出任何异常,此成员函数不起作用。
时间复杂
常量即O(1),如果当前集合alloc与x的分配器不同则为expcept
例子 (Example)
以下示例显示了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