Source of map07.cpp


  1: //map07.cpp

  3: #include <iostream>
  4: #include <map>
  5: #include <utility>
  6: using namespace std;

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

 14:     //Create an array of pairs
 15:     pair<char, int> a[] =
 16:     {
 17:         pair<char, int>('A', 65),
 18:         pair<char, int>('B', 66),
 19:         pair<char, int>('C', 67),
 20:         pair<char, int>('D', 68),
 21:         pair<char, int>('E', 69)
 22:     };
 23:     //Initialize a map with values from the array
 24:     map<char, int> m(a, a+5);

 26:     cout << "\nFirst we use a reverse iterator to display the map "
 27:         "contents\nin descending key order.";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 29:     cout << endl;
 30:     map<char, int>::reverse_iterator r_p;
 31:     for (r_p = m.rbegin(); r_p != m.rend(); ++r_p)
 32:         cout << r_p->first << " has an ASCII code of "
 33:             << r_p->second << ".\n";
 34:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 36:     cout << "\nThen we use a reverse iterator to display the map "
 37:         "contents\nin ascending key order.";
 38:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 39:     r_p = m.rend();
 40:     cout << endl;
 41:     while (r_p != m.rbegin())
 42:     {
 43:         --r_p;
 44:         cout << r_p->first << " has an ASCII code of "
 45:             << r_p->second << ".\n";
 46:     }
 47:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 48: }