vector::vector
描述 (Description)
C ++移动构造函数std::vector::vector()使用移动语义构造具有其他内容的容器。
如果未提供alloc ,则通过从属于其他的分配器的move-construction获得分配器。
声明 (Declaration)
以下是move costructor std :: vector :: vector()形式std :: vector header的声明。
C++11
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
参数 (Parameters)
x - 相同类型的另一个向量容器。
返回值
构造函数永远不会返回值。
异常 (Exceptions)
该成员函数从不抛出异常。
时间复杂
线性即O(n)
例子 (Example)
以下示例显示了move构造函数std :: vector :: vector()的用法。
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
/* create fill constructor */
vector<int> v1(5, 123);
cout << "Elements of vector v1 before move constructor" << endl;
for (int i = 0; i < v1.size(); ++i)
cout << v1[i] << endl;
/* create constructor using move semantics */
vector<int> v2(move(v1));
cout << "Elements of vector v1 after move constructor" << endl;
for (int i = 0; i < v1.size(); ++i)
cout << v1[i] << endl;
cout << "Element of vector v2" << endl;
for (int i = 0; i < v2.size(); ++i)
cout << v2[i] << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Elements of vector v1 before move constructor
123
123
123
123
123
Elements of vector v1 after move constructor
Element of vector v2
123
123
123
123
123