stl swap函数_vector :: swap()函数以及C ++ STL中的示例
stl swap函數
C ++ vector :: swap()函數 (C++ vector::swap() function)
vector::swap() is a library function of "vector" header, it is used to swap the content of the vectors, it is called with a vector and accepts another vector as an argument and swaps their content. (Sizes of both of the vectors may differ).
vector :: swap()是“ vector”頭文件的庫函數,用于交換向量的內容,用向量調用它并接受另一個向量作為參數并交換其內容。 (兩個向量的大小可能不同)。
Note: To use vector, include <vector> header.
注意:要使用向量,請包含<vector>標頭。
Syntax of vector::swap() function
vector :: swap()函數的語法
vector::swap(vector& v);Parameter(s): v – It is another vector to be swapped content with current vector.
參數: v –這是另一個要與當前向量交換內容的向量。
Return value: void – It returns nothing.
返回值: void –不返回任何內容。
Example:
例:
Input:vector<int> v1{ 10, 20, 30, 40, 50 };vector<int> v2{ 100, 200, 300 };//swapping content of the vectorsv1.swap(v2);Output://if we print the valuesv1: 100 200 300v2: 10 20 30 40 50C ++程序演示vector :: swap()函數的示例 (C++ program to demonstrate example of vector::swap() function)
//C++ STL program to demonstrate example of //vector::erase() function#include <iostream> #include <vector> using namespace std;int main() {//vector declarationvector<int> v1{ 10, 20, 30, 40, 50 };vector<int> v2{ 100, 200, 300 };//printing the sizes and values of the vectorscout << "before swap() call..." << endl;cout << "size of v1: " << v1.size() << endl;cout << "size of v2: " << v2.size() << endl;cout << "v1: ";for (int x : v1)cout << x << " ";cout << endl;cout << "v2: ";for (int x : v2)cout << x << " ";cout << endl;//swapping the content of the vectorsv1.swap(v2);//printing the sizes and values of the vectorscout << "after swap() call..." << endl;cout << "size of v1: " << v1.size() << endl;cout << "size of v2: " << v2.size() << endl;cout << "v1: ";for (int x : v1)cout << x << " ";cout << endl;cout << "v2: ";for (int x : v2)cout << x << " ";cout << endl;return 0; }Output
輸出量
before swap() call... size of v1: 5 size of v2: 3 v1: 10 20 30 40 50 v2: 100 200 300 after swap() call... size of v1: 3 size of v2: 5 v1: 100 200 300 v2: 10 20 30 40 50Reference: C++ vector::swap()
參考: C ++ vector :: swap()
翻譯自: https://www.includehelp.com/stl/vector-swap-function-with-example.aspx
stl swap函數
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的stl swap函数_vector :: swap()函数以及C ++ STL中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 固原治疗精子活率低最好的医院推荐
- 下一篇: 金属探测仪多少钱啊?