#include <boost/swap.hpp>
#include <boost/array.hpp>
#include <iostream>
using namespace std;
int main()
{
char c1 = 'x';
char c2 = 'y';
cout<< "The characters before swapping :"<<"c1 : "<<c1<<", c2 : "<<c2<<endl;
boost::swap(c1, c2); //This call swaps the characters
cout<< "The characters after swapping :"<<"c1 : "<<c1<<", c2 : "<<c2<<endl;
boost::array<int, 1> a1{{1}}; //Array with single element
boost::array<int, 1> a2{{2}};
cout<< "The array before swapping :"<< a1[0] <<" "<< a2[0] << '\n';
boost::swap(a1, a2);
cout << "The array before swapping :" << a1[0] <<" "<< a2[0] << '\n';
boost::array<int, 4> a3{{1,2,3,4}}; //Array with many elements
boost::array<int, 4> a4{{10,11,12,13}};
//Both Array before swapping
cout<<endl<<"The array before swapping : "<<endl;
for(int i=0;i<4;i++){
cout<<"a3["<< i <<"] :" << a3[i] <<" a4[" << i <<"] :"<< a4[i] << '\n';
}
boost::swap(a3, a4);
cout<<endl<<"The array after swapping : "<<endl;
for(int i=0;i<4;i++){
cout<<"a3["<< i <<"] :" << a3[i] <<" a4[" << i <<"] :"<< a4[i] << '\n';
}
}
Output:
The characters before swapping :c1 : x, c2 : y
The characters after swapping :c1 : y, c2 : x
The array before swapping :1 2
The array before swapping :2 1
The array before swapping :
a3[0] :1 a4[0] :10
a3[1] :2 a4[1] :11
a3[2] :3 a4[2] :12
a3[3] :4 a4[3] :13
The array after swapping :
a3[0] :10 a4[0] :1
a3[1] :11 a4[1] :2
a3[2] :12 a4[2] :3
a3[3] :13 a4[3] :4
Comments