Mega Code Archive

 
Categories / C++ / Data Structure
 

Demonstrating a multimap

#include <iostream> #include <map> #include <string> using namespace std; int main() {   multimap<string, string> names;   string n;   names.insert(pair<string, string>("Z", "F"));   names.insert(pair<string, string>("Z", "A"));   names.insert(pair<string, string>("S", "T"));   names.insert(pair<string, string>("S", "A"));   names.insert(pair<string, string>("S", "J"));   names.insert(pair<string, string>("D", "H"));   names.insert(pair<string, string>("D", "W"));   names.insert(pair<string, string>("D", "R"));   multimap<string, string>::iterator p;      cout << "Enter last name: ";   cin >> n;   p = names.find(n);   if(p != names.end()) { // found a name     do {       cout << n << ", " << p->second;       cout << endl;       p++;     } while (p != names.upper_bound(n));   }   else{     cout << "Name not found.\n";   }   return 0; }