Source of map14.cpp


  1: //map14.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the upper_bound() member "
 11:         "function\nof the map interface.";
 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>('D', 68),
 18:         pair<char, int>('E', 69),
 19:         pair<char, int>('F', 70),
 20:         pair<char, int>('G', 71),
 21:         pair<char, int>('H', 72),
 22:         pair<char, int>('I', 73),
 23:         pair<char, int>('J', 74),
 24:         pair<char, int>('K', 75),
 25:         pair<char, int>('L', 76),
 26:         pair<char, int>('M', 77)
 27:     };

 29:     cout << "\nSuppose we begin with a map containing the "
 30:         "following pairs:\n";
 31:     map<char, int> m(a, a+10);
 32:     map<char, int>::iterator p = m.begin();
 33:     while (p != m.end())
 34:     {
 35:         cout << p->first << " " << p->second << endl;
 36:         ++p;
 37:     }
 38:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 40:     cout << "\nWe use the upper_bound() function to find the upper "
 41:         "bound for a key in the\nmap, then for a key that precedes any "
 42:         "key in the map, and finally for a key\nthat follows all keys "
 43:         "in the map.";
 44:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 46:     cout << "\nThe upper bound for the key H is the position of "
 47:         "this pair:\n";
 48:     p = m.upper_bound('H');
 49:     cout << p->first << " " << p->second;
 50:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 52:     cout << "\nThe upper bound for the key A is the position of "
 53:         "this pair:\n";
 54:     p = m.upper_bound('A');
 55:     cout << p->first << " " << p->second;
 56:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 58:     p = m.upper_bound('T');
 59:     if (p == m.end())
 60:         cout << "\nThe upper bound for the key T is the "
 61:             "one-past-the-last position.";
 62:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 63: }