Mega Code Archive
Categories
/
C++ Tutorial
/
Map Multimap
Uses a multimap to store names and phone numbers
#include
#include
#include
using namespace std; void shownumbers(const char *n, multimap
mp); int main() { multimap
phonemap; // Insert elements by using operator[]. phonemap.insert(pair
("A", "B: 555-1111")); phonemap.insert(pair
("C", "D: 555-1234")); phonemap.insert(pair
("E", "F: 555-1122")); phonemap.insert(pair
("J", "K: 314 555-2222")); phonemap.insert(pair
("L", "M: 314 555-3322")); phonemap.insert(pair
("O", "P: 666 555-9876")); phonemap.insert(pair
("Q", "R: 555 555-1010")); phonemap.insert(pair
("S", "T: 444 555-9999")); shownumbers("A", phonemap); shownumbers("C", phonemap); shownumbers("E", phonemap); int count = phonemap.erase("A"); cout << count << " elements have been removed.\n\n"; shownumbers("Ken", phonemap); return 0; } // Show all numbers for a given name. void shownumbers(const char *n, multimap
mmp) { multimap
::iterator itr; // Find the first matching key. itr = mmp.find(n); if(itr != mmp.end()) { cout << "the numbers for " << n << ": " << endl; do { cout << " " << itr->second << endl; ++itr; } while (itr != mmp.upper_bound(n)); } else cout << "No entry for " << n << " found." << endl; }