Source of map08.cpp


  1: //map08.cpp

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

  8: void DisplayASCIICodes
  9: (
 10:     const map<char, int>& asciiCodes //in
 11: );
 12: /**<
 13: Display pairs of ASCII codes and their corresponding characters.
 14: @return void
 15: @param asciiCodes A map of char/int pairs.
 16: @pre asciiCodes has been initialized.
 17: @post The componets of the map asciiCodes have been displayed,
 18: one per line, with the ASCII code of any character in the map
 19: following that character on the line.
 20: */

 22: int main()
 23: {
 24:     cout << "\nThis program illustrates a const map iterator.";
 25:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 27:     //Create an array of pairs
 28:     pair<char, int> a[] =
 29:     {
 30:         pair<char, int>('A', 65),
 31:         pair<char, int>('B', 66),
 32:         pair<char, int>('C', 67),
 33:         pair<char, int>('D', 68),
 34:         pair<char, int>('E', 69)
 35:     };
 36:     //Initialize a map with values from the array
 37:     map<char, int> asciiCodes(a, a+5);

 39:     DisplayASCIICodes(asciiCodes);
 40:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 41: }

 43: void DisplayASCIICodes
 44: (
 45:     const map<char, int>& asciiCodes //in
 46: )
 47: {
 48:     //const input parameter requires const_iterator
 49:     map<char, int>::const_iterator p = asciiCodes.begin();
 50:     cout << endl;
 51:     while (p != asciiCodes.end())
 52:     {
 53:         cout << p->first << " has an ASCII code of " << p->second << ".\n";
 54:         ++p;
 55:     }
 56: }