C++ vector swap() 函数
示例
交换两个向量的内容:
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
vector<string> fruits = {"Apple", "Banana", "Cherry", "Orange"};
cars.swap(fruits);
cout << "Cars:
";
for (string car : cars) {
cout << car << "
";
}
cout << "
Fruits:
";
for (string fruit : fruits) {
cout << fruit << "
";
}
亲自试一试 »
定义和用法
swap() 函数交换两个向量的内容。调用 swap() 函数后,每个向量的内容都将变为之前另一个向量的内容。
语法
vector.swap(vector other);
参数值
| 参数 | 描述 |
|---|---|
| other | 必填。要与之交换内容的向量。 |
相关页面
阅读我们的向量教程,了解更多关于向量的知识。

