This C++ program swaps values using swap() algorithm. The elements are passed to the function as parameters by reference and the values of the elements are then swapped by the function swap() defined in algorithm library.
Here is the source code of the C++ program which swaps values using swap() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to swap values using swap() algorithm
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int A = 10, B = 20;
cout << "Before swap" << endl;
cout << "A = " << A << ", B = " << B << endl;
swap(A, B);
cout << "After swap" << endl;
cout << "A = " << A << ", B = " << B << endl;
}
$ a.out Before swap A = 10, B = 20 After swap A = 20, B = 10
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Check Computer Science Books
- Practice Computer Science MCQs