swap() Function in C++

This C++ program swaps elements of containers using swap() algorithm. The program initializes the elements of the container to be swapped, the containers are passed to the function by reference and the elements of the containers are swapped.

Here is the source code of the C++ program which swaps elements of two container using swap() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to swap elements of containers using swap() algorithm
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <string>
  8. #include <iomanip>
  9. using namespace std;
  10.  
  11. void print(vector <string> vs)
  12. {   
  13.     vector <string>::iterator i;
  14.     for(i = vs.begin(); i != vs.end(); i++)
  15.     {
  16.         cout << left << setw(10) << *i;
  17.     }
  18.     cout << endl;
  19. }
  20.  
  21. int main()
  22. {
  23.     string sarr[] = {"Belarus", "Croatia", "Denmark", "Finland", "Bhutan", "China", "India", "Japan"};
  24.     int slen = sizeof(sarr) / sizeof(string);
  25.  
  26.     vector <string> va(sarr, sarr + 4);
  27.     cout << "Asian Countries    : ";
  28.     print(va);
  29.     vector <string> ve(sarr + 4, sarr + slen);
  30.     cout << "European Countries : ";
  31.     print(ve);
  32.     cout << "\nCountries are in wrong lists. They are to be swapped!" << endl;
  33.     swap(va, ve);
  34.     cout << "\nAsian Countries    : ";
  35.     print(va);
  36.     cout << "European Countries : ";
  37.     print(ve);
  38.     cout << "\nNow they are in right lists!" << endl;
  39. }

$ a.out
Asian Countries    : Belarus   Croatia   Denmark   Finland   
European Countries : Bhutan    China     India     Japan     
 
Countries are in wrong lists. They are to be swapped!
 
Asian Countries    : Bhutan    China     India     Japan     
European Countries : Belarus   Croatia   Denmark   Finland   
 
Now they are in right lists!

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.