Source of vector07.cpp


  1: //vector07.cpp

  3: #include <iostream>
  4: #include <vector>
  5: using namespace std;

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates reverse iterators of the "
 10:         "vector class,\nas well as member functions rbegin() and "
 11:         "rend().";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     cout << "\nHere are the contents of a vector of size 12:\n";
 15:     int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24};
 16:     vector<int> v(a, a+12);
 17:     vector<int>::iterator p = v.begin();
 18:     while (p != v.end()) cout << *p++ << " ";
 19:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 21:     cout << "\nNow using a reverse iterator to display\n"
 22:         "the vector components in reverse order:\n";
 23:     vector<int>::reverse_iterator r_p = v.rbegin();
 24:     while (r_p != v.rend()) cout << *r_p++ << " ";
 25:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 27:     cout << "\nNow using a reverse iterator to display\n"
 28:         "the vector components in forward order:\n";
 29:     r_p = v.rend();
 30:     while (r_p != v.rbegin()) cout << *--r_p << " ";
 31:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 33:     cout << "\nNow constructing a new vector containing the "
 34:         "values from 18 down to 6 from\nthe first vector, and "
 35:         "then displaying the values from this new vector.\n";
 36:     vector<int> v1(v.rbegin()+3, v.rbegin()+10);
 37:     p = v1.begin();
 38:     while (p != v1.end()) cout << *p++ << " ";
 39:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 40: }