1: //swap1a.cpp 3: #include <iostream> 4: #include <vector> 5: #include <string> 6: #include <algorithm> 7: using namespace std; 9: int main() 10: { 11: cout << "\nThis program illustrates the use of the STL swap() " 12: "algorithm to swap\ninteger values, double values, and string " 13: "values in simple variables.\nIt also illustrates the swapping " 14: "of integer values in a vector, using\nthree different ways to " 15: "access the values."; 16: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 18: cout << "\nSwapping integer values in simple variables:"; 19: int i1 = 12, i2 = -3; 20: cout << "\nBefore swap(i1, i2) i1 = " << i1 << "\ti2 = " << i2; 21: swap(i1, i2); 22: cout << "\nAfter swap(i1, i2) i1 = " << i1 << "\ti2 = " << i2; 23: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 25: cout << "\nSwapping double values in simple variables:"; 26: double x1 = 4.3, x2 = 17.5; 27: cout << "\nBefore swap(x1, x2) x1 = " << x1 << "\tx2 = " << x2; 28: swap(x1, x2); 29: cout << "\nAfter swap(x1, x2) x1 = " << x1 << "\tx2 = " << x2; 30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 32: cout << "\nSwapping string values in simple variables:"; 33: string s1("Hello"), s2("Good-bye"); 34: cout << "\nBefore swap(s1, s2) s1 = " << s1 << " s2 = " << s2; 35: swap(s1, s2); 36: cout << "\nAfter swap(s1, s2) s1 = " << s1 << " s2 = " << s2; 37: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 39: cout << "\nNow we display the values from a vector of integers. " 40: "Then we swap the first\nand last values, the second and second " 41: "last values, and the third and third\nlast values. Each time " 42: "we do a swap, we use a different way of accessing the\nvalues " 43: "being swapped."; 44: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 46: int a[] = {1, 2, 3, 4, 5, 6, 7, 8}; 47: vector<int> v(a, a+8); 48: cout << "\nHere are the integer values from the vector:\n"; 49: for (vector<int>::size_type i=0; i<v.size(); i++) 50: cout << v.at(i) << " "; 51: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 53: cout << "\nFirst we make the call swap(v.at(0), v.at(v.size()-1)."; 54: swap(v.at(0), v.at(v.size()-1)); 55: cout << "\nHere are the vector values with the first and last " 56: "ones swapped:\n"; 57: for (vector<int>::size_type i=0; i<v.size(); i++) 58: cout << v.at(i) << " ";; 59: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 61: cout << "\nNext we make the call swap(v[1], v[v.size()-2])."; 62: swap(v[1], v[v.size()-2]); 63: cout << "\nHere are the vector values with the 2nd and 2nd-last " 64: "ones swapped:\n"; 65: for (vector<int>::size_type i=0; i<v.size(); i++) 66: cout << v.at(i) << " ";; 67: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 69: cout << "\nFinally we make the call " 70: "swap(*(v.begin()+2), *(v.end()-3))."; 71: swap(*(v.begin()+2), *(v.end()-3)); 72: cout << "\nHere are the vector values with the 3rd and 3rd-last " 73: "ones swapped:\n"; 74: for (vector<int>::size_type i=0; i<v.size(); i++) 75: cout << v.at(i) << " ";; 76: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 77: }