Source of map09.cpp


  1: //map09.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the count() member function "
 11:         "for maps.";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     cout << "\nFirst we put the first five pairs of capital letters "
 15:         "and their ASCII codes\ninto a map. In this case the capital "
 16:         "letters are the keys and the ASCII\ncodes are the values in "
 17:         "the key/value pairs of the map. Then we count the\nnumber of "
 18:         "times a pair with a given key (a capital letter entered by "
 19:         "the\nuser) appears in the map.";
 20:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 22:     //Create an array of pairs
 23:     pair<char, int> a[] =
 24:     {
 25:         pair<char, int>('A', 65),
 26:         pair<char, int>('B', 66),
 27:         pair<char, int>('C', 67),
 28:         pair<char, int>('D', 68),
 29:         pair<char, int>('E', 69)
 30:     };
 31:     //Initialize a map with values from the array
 32:     map<char, int> m(a, a+5);

 34:     char ch;
 35:     cout << "\nEnter a capital letter: ";
 36:     cin >> ch;  cin.ignore(80, '\n');
 37:     cout << "The number of times a component with key " << ch
 38:         << " appears in the map is " << m.count(ch) << ".";
 39:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 40: }