Source of map06.cpp


  1: //map06.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the make_pair() "
 11:         "function to create pairs\nfor an array, which are then used "
 12:         "to construct a map. A second map is then\nconstructed, using "
 13:         "all but the first and last pairs from the first map, and\nthe "
 14:         "contents of the smaller second map are then displayed.";
 15:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 29:     map<char, int>::iterator p_begin = m1.begin();
 30:     map<char, int>::iterator p_end = m1.end();
 31:     ++p_begin;
 32:     --p_end;
 33:     map<char, int> m2(p_begin, p_end);

 35:     cout << "\nHere is output which includes the contents of that "
 36:         "smaller second map:\n";
 37:     for (char ch='B'; ch<='D'; ++ch)
 38:         cout << ch << " has an ASCII code of " << m2[ch] << ".\n";
 39:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 40: }