vector::operator=
描述 (Description)
C ++函数std::vector::operator=()将一个向量的内容移动到另一个向量中,并在必要时修改大小。
声明 (Declaration)
以下是std :: vector :: operator =()函数形式std :: vector标头的声明。
C++98
vector& operator= (vector&& x);
参数 (Parameters)
x - 相同类型的另一个向量容器。
返回值
返回此指针。
异常 (Exceptions)
该成员函数从不抛出异常。
时间复杂
线性即O(n)
例子 (Example)
以下示例显示了std :: vector :: operator =()函数的用法。
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
vector<int> v1 = {1, 2, 3, 4, 5};
vector<int> v2;
/* assignment using move construct */
v2 = move(v1);
for (int i = 0; i < v2.size(); ++i)
cout << v2[i] << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
1
2
3
4
5