Mega Code Archive
Categories
/
C++
/
Data Structure
Use a multimap to create the phone directory
#include
#include
#include
using namespace std; class name { string str; public: name() { str = ""; } name(string s) { str = s; } string get() { return str; } }; bool operator<(name a, name b) { // Define less than relative to name objects. return a.get() < b.get(); } class phoneNum { string str; public: phoneNum() { str = ""; } phoneNum(string s) { str = s; } string get() { return str; } }; int main() { multimap
directory; directory.insert(pair
(name("T"), phoneNum("555-4533"))); directory.insert(pair
(name("T"), phoneNum("555-9999"))); directory.insert(pair
(name("C"), phoneNum("555-9678"))); string str; cout << "Enter name: "; cin >> str; multimap
::iterator p; p = directory.find(str); if(p != directory.end()) { do { cout << "Phone number: " << p->second.get(); cout << endl; p++; } while(p != directory.upper_bound(str)); } else cout << "Name not in directory.\n"; return 0; }