1: //multimap02.cpp 3: #include <iostream> 4: #include <map> 5: #include <string> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program illustrates how the equal_range() " 11: "member function can be used\nto find a range of equal " 12: "key values in a multimap."; 13: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 15: cout << "\nHere are the names in our multimap. Note that " 16: "the lastname is the key, and\nthe first name is the " 17: "corresponding value:\n"; 18: multimap<string, string> names; 19: names.insert(pair<string, string>("Jones", "Fred")); 20: names.insert(pair<string, string>("Jones", "Alice")); 21: names.insert(pair<string, string>("Smith", "Tom")); 22: names.insert(pair<string, string>("Smith", "Alicia")); 23: names.insert(pair<string, string>("Smith", "Jon")); 24: names.insert(pair<string, string>("Doe", "Harvey")); 25: names.insert(pair<string, string>("Doe", "Wilma")); 26: names.insert(pair<string, string>("Doe", "Rachel")); 27: names.insert(pair<string, string>("Johnson", "J.T.")); 28: multimap<string, string>::iterator p1 = names.begin(); 29: while (p1 != names.end()) 30: { 31: cout << p1->first << ", " << p1->second << endl; 32: ++p1; 33: } 34: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 36: string name; 37: cout << "\nEnter a last name to see all the folks with that last name: "; 38: cin >> name; cin.ignore(80, '\n'); 40: pair<multimap<string, string>::iterator, 41: multimap<string, string>::iterator> p2; 42: if (names.find(name) != names.end()) 43: { 44: p2 = names.equal_range(name); 45: while (p2.first != p2.second) 46: { 47: cout << p2.first->first << ", " << p2.first->second << endl; 48: ++p2.first; 49: } 50: } 51: else 52: cout << "That name was not found.\n"; 53: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 54: }