Source of multimap01.cpp


  1: //multimap01.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the difference in behavior when "
 11:         "duplicate\nentries are inserted into a map and a multimap.";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 14:     cout << "\nFirst we create an empty map and try to put five people "
 15:         "in it,\ntwo named Jones and three named Smith.";
 16:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 17:     map<string, string> namesMap;
 18:     namesMap.insert(pair<string, string>("Jones", "Fred"));
 19:     namesMap.insert(pair<string, string>("Jones", "Alice"));
 20:     namesMap.insert(pair<string, string>("Smith", "Tom"));
 21:     namesMap.insert(pair<string, string>("Smith", "Alicia"));
 22:     namesMap.insert(pair<string, string>("Smith", "Jon"));
 23:     map<string, string>::iterator i = namesMap.begin();

 25:     cout << "\nThe size of the our map is " << namesMap.size()
 26:         << ". Here are the people in it:\n";
 27:     while (i != namesMap.end())
 28:     {
 29:         cout << i->first << " " << i->second << endl;
 30:         ++i;
 31:     }
 32:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 34:     cout << "\nNext we create an empty multimap and try to put the same "
 35:         "five people in it.";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 37:     multimap<string, string> names;
 38:     names.insert(pair<string, string>("Jones", "Fred"));
 39:     names.insert(pair<string, string>("Jones", "Alice"));
 40:     names.insert(pair<string, string>("Smith", "Tom"));
 41:     names.insert(pair<string, string>("Smith", "Alicia"));
 42:     names.insert(pair<string, string>("Smith", "Jon"));
 43:     multimap<string, string>::iterator p = names.begin();

 45:     cout << "\nThe size of the our multimap is " << names.size()
 46:         << ". Here are the people in it:\n";
 47:     while (p != names.end())
 48:     {
 49:         cout << p->first << " " << p->second << endl;
 50:         ++p;
 51:     }
 52:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 53: }