最近看到了数组的的交换用法:
记录如下:
在vector里可以交换的元素个数不一致也可以的。
// swap arrays
#include <iostream>
#include <array>
#include<vector>
using namespace std;
int main()
{
array<int, 5> first = { 10, 20, 30, 40, 50 };
array<int, 5> second = { 11, 22, 33, 44, 55};
first.swap(second);
int a[] = { 20,11,52,36,89};
vector<int>arr(a,a+5);
vector<int>brr{ 11,25,63,96,85,89 };
cout << "交换前:";
for (int x : arr)cout << ' ' << x;
cout << '\n';
arr.swap(brr);
cout << "交换后:";
for (int x : arr)cout << ' ' << x;
cout << '\n';
cout << "first:";
for (int& x : first) cout << ' ' << x;
cout << '\n';
cout << "second:";
for (int& x : second) cout << ' ' << x;
cout << '\n';
system("pause");
return 0;
}
输出:
交换前: 20 11 52 36 89
交换后: 11 25 63 96 85 89
first: 11 22 33 44 55
second: 10 20 30 40 50