Source of swap_ranges1a.cpp


  1: //swap_ranges1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <deque>
  6: #include <algorithm>
  7: using namespace std;

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the STL "
 12:         "swap_ranges() algorithm to\ninterchange two ranges of "
 13:         "integers in two different containers.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a1[] = {1, 3, 5, 7, 9, 11, 13, 15, 19, 21, 23, 25};
 17:     vector<int> v(a1, a1+12);
 18:     int a2[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 22, 24, 26};
 19:     deque<int> d(a2, a2+12);

 21:     cout << "\nHere are the values in a vector:\n";
 22:     for (vector<int>::size_type i=0; i<v.size(); i++)
 23:         cout << v.at(i) << " ";
 24:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 26:     cout << "\nHere are the values in a deque:\n";
 27:     for (deque<int>::size_type i=0; i<d.size(); i++)
 28:         cout << d.at(i) << " ";
 29:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 31:     cout << "\nNow we swap five values from the vector, starting "
 32:         "at the fourth value,\nwith five values from the deque, "
 33:         "starting at its sixth value.";
 34:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 36:     deque<int>::iterator p;
 37:     p = swap_ranges(v.begin()+3, v.begin()+8, d.begin()+5);

 39:     cout << "\nHere are the values in the vector after the swap:\n";
 40:     for (vector<int>::size_type i=0; i<v.size(); i++)
 41:         cout << v.at(i) << " ";
 42:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 44:     cout << "\nHere are the values in the deque after the swap:\n";
 45:     for (deque<int>::size_type i=0; i<d.size(); i++)
 46:         cout << d.at(i) << " ";
 47:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 49:     cout << "\nAnd finally, the value pointed to by the iterator "
 50:         "returned by the algorithm\nis the value " << *p
 51:         << " in the deque.";
 52:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 53: }