Source of map11.cpp


  1: //map11.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the insert() 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.";
 18:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 32:     cout << "\nHere are the pairs in the map:\n";
 33:     map<char, int>::iterator p = m1.begin();
 34:     while (p != m1.end())
 35:     {
 36:         cout << p->first << " " << p->second << endl;
 37:         ++p;
 38:     }
 39:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 41:     cout << "\nNow we try to insert Z and its ASCII code 90 into the map.";
 42:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 43:     pair<map<char, int>::iterator, bool> insertResult;
 44:     insertResult = m1.insert(pair<char, int>('Z', 90));
 45:     if (insertResult.second == true)
 46:     {
 47:         cout << "\nSuccess! The following pair is now in the map:\n";
 48:         cout << insertResult.first->first << " "
 49:             << insertResult.first->second;
 50:     }
 51:     else
 52:     {
 53:         cout << "\nRats! That insertion did not succeed.";
 54:         cout << "\nWhy not? Because the following pair was already "
 55:             "in the map:\n";
 56:         cout << insertResult.first->first << " "
 57:             << insertResult.first->second;
 58:     }
 59:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 61:     cout << "\nNext we try to insert D and its ASCII code 68 into the map.";
 62:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 63:     insertResult = m1.insert(pair<char, int>('D', 68));
 64:     if (insertResult.second == true)
 65:     {
 66:         cout << "\nSuccess! The following pair is now in the map:\n";
 67:         cout << insertResult.first->first << " "
 68:             << insertResult.first->second;
 69:     }
 70:     else
 71:     {
 72:         cout << "\nRats! That insertion did not succeed.";
 73:         cout << "\nWhy not? Because the following pair was already "
 74:             "in the map:\n";
 75:         cout << insertResult.first->first << " "
 76:             << insertResult.first->second;
 77:     }
 78:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 80:     cout << "\nNow we try to insert pairs containing the capital letters "
 81:         "T, H and\nD (again), along with their ASCII codes, in that order.";
 82:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 83:     map<char, int>::iterator p1, p2, p3;
 84:     p1 = m1.insert(m1.begin(), make_pair('T', 84));
 85:     p2 = m1.insert(m1.end(), make_pair('H', 72));
 86:     p3 = m1.insert(m1.begin(), make_pair('D', 68));
 87:     cout << "\nHere are the pairs in the map after those three "
 88:         "attempted insertions:\n";
 89:     p = m1.begin();
 90:     while (p != m1.end())
 91:     {
 92:         cout << p->first << " " << p->second << endl;
 93:         ++p;
 94:     }
 95:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 97:     cout << "\nAnd the pairs pointed to by the returned iterators "
 98:         "\nof these three calls to insert() are:\n";
 99:     cout << p1->first << " " << p1-> second << endl;
100:     cout << p2->first << " " << p2-> second << endl;
101:     cout << p3->first << " " << p3-> second << endl;
102:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

104:     cout << "\nFinally we create another map containing pairs with "
105:         "the capital letters from\nC to J and their corresponding ASCII "
106:         "codes. Then we try insert all of these\ninto the previous map "
107:         "using the insert() function. Some of them will go in,\nand some "
108:         "won't, because they are already there.";
109:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

111:     //Create an array of pairs
112:     pair<char, int> a2[] =
113:     {
114:         pair<char, int>('C', 67),
115:         pair<char, int>('D', 68),
116:         pair<char, int>('E', 69),
117:         pair<char, int>('F', 70),
118:         pair<char, int>('G', 71),
119:         pair<char, int>('H', 72),
120:         pair<char, int>('I', 73),
121:         pair<char, int>('J', 74),
122:     };
123:     map<char, int> m2(a2, a2+8);
124:     m1.insert(m2.begin(), m2.end());

126:     cout << "\nHere are the pairs in the map after these "
127:         "attempted insertions:\n";
128:     p = m1.begin();
129:     while (p != m1.end())
130:     {
131:         cout << p->first << " " << p->second << endl;
132:         ++p;
133:     }
134:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
135: }