Source of map17.cpp


  1: //map17.cpp

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

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the comparison of maps using "
 12:         "the relational operators.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 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:     map<char, int> m1(a, a+4);
 24:     map<char, int> m2(a, a+5);
 25:     map<char, int> m3(a, a+5);

 27:     cout << "\nWe begin by creating three maps.";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     cout << "\nHere are the pairs in the first map:\n";
 31:     map<char, int>::iterator p = m1.begin();
 32:     while (p != m1.end())
 33:     {
 34:         cout << p->first << " " << p->second << endl;
 35:         ++p;
 36:     }
 37:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 39:     cout << "\nHere are the pairs in the second map:\n";
 40:     p = m2.begin();
 41:     while (p != m2.end())
 42:     {
 43:         cout << p->first << " " << p->second << endl;
 44:         ++p;
 45:     }
 46:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 48:     cout << "\nThe contents of the third map are the same as the second.";
 49:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 51:     cout << "\nNow we perform and show the results of some "
 52:         "map comparisons.";
 53:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 55:     cout << "\nm1 < m2  = " << boolalpha << (m1 < m2);
 56:     cout << "\nm1 <= m2 = " << boolalpha << (m1 <= m2);
 57:     cout << "\nm1 > m2  = " << boolalpha << (m1 > m2);
 58:     cout << "\nm1 >= m2 = " << boolalpha << (m1 >= m2);
 59:     cout << "\nm2 == m3 = " << boolalpha << (m2 == m3);
 60:     cout << "\nm2 != m3 = " << boolalpha << (m2 != m3);
 61:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 62: }