Source of map10.cpp


  1: //map10.cpp

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

  7: int main()
  8: {
  9:     cout << "\nThis program illlusrates the find() member function of "
 10:         "the map class.";
 11:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 13:     cout << "\nFirst we put all pairs of capital letters and their ASCII "
 14:         "codes into a map.\nIn this case the capital letters are the "
 15:         "keys and the ASCII codes are the\nvalues in the key/value pairs "
 16:         "of the map.";
 17:     map<char, int> m;
 18:     for (char ch='A'; ch<='Z'; ++ch) m[ch] = (int)ch;
 19:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 21:     cout << "\nNow we can find the ASCII code for any capital letter.";
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 23:     cout << "\nEnter a capital, to see its ASCII code: ";
 24:     char ch;
 25:     cin >> ch;  cin.ignore(80, '\n');
 26:     map<char, int>::iterator p = m.find(ch);
 27:     if (p != m.end())
 28:         cout << "The ASCII code of " << p->first 
 29:              << " is " << p->second << ".\n";
 30:     else
 31:         cout << "That character does not appear to be a capital letter.\n";
 32:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 33: }