In the following code example, the contents of the two arrays are swapped. The elements from array A are in array B after the swap function has been executed.
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 9> firstArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};
array<int, 9> secondArray = {10, 20, 30, 40, 50, 60, 70, 80, 90};
cout << "before swapping" << endl;
cout << "first array" << endl;
for (int i = 0; i < 9; i++) {
cout << firstArray.at(i) << " ";
}
cout << "\nsecond array" << endl;
for (int i = 0; i < 9; i++) {
cout << secondArray.at(i) << " ";
}
firstArray.swap(secondArray);
cout << "\n\nafter swapping" << endl;
cout << "first array" << endl;
for (int i = 0; i < 9; i++) {
cout << firstArray.at(i) << " ";
}
cout << "\nsecond array" << endl;
for (int i = 0; i < 9; i++) {
cout << secondArray.at(i) << " ";
}
return 0;
}
before swapping
first array
1 2 3 4 5 6 7 8 9
second array
10 20 30 40 50 60 70 80 90
after swapping
first array
10 20 30 40 50 60 70 80 90
second array
1 2 3 4 5 6 7 8 9