Source of map02.cpp


  1: //map02.cpp

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

  7: int main()
  8: {
  9:     cout << "\nThis program illustrates the map copy constructor and "
 10:         "the map overloaded\nassignment operator. The program first "
 11:         "creates a map with three key/value\npairs. Next it makes a copy "
 12:         "of this map and displays the contents of the\ncopy. Finally, it "
 13:         "assigns the original map to an empty, and then displays\nthe "
 14:         "contents of the map that received the assignment.";
 15:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 17:     map<char, int> m1;
 18:     m1['A'] = 65;
 19:     m1['B'] = 66;
 20:     m1['C'] = 67;

 22:     cout << "\nSo ... first the contents of the copy:";
 23:     map<char, int> m2(m1);
 24:     cout << "\nIf the component key is A, the component value is "
 25:         << m2['A'] << ".";
 26:     cout << "\nIf the component key is B, the component value is "
 27:         << m2['B'] << ".";
 28:     cout << "\nIf the component key is C, the component value is "
 29:         << m2['C'] << ".";
 30:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 32:     cout << "\nAnd finally, the contents of the assignment recipient:";
 33:     map<char, int> m3;
 34:     m3 = m1;
 35:     cout << "\nIf the component key is A, the component value is "
 36:         << m3['A'] << ".";
 37:     cout << "\nIf the component key is B, the component value is "
 38:         << m3['B'] << ".";
 39:     cout << "\nIf the component key is C, the component value is "
 40:         << m3['C'] << ".";
 41:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 42: }