Use a multimap to create the phone directory.
#include <iostream>
#include <map>
#include <string>
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<name, phoneNum> directory;
directory.insert(pair<name, phoneNum>(name("T"), phoneNum("555-4533")));
directory.insert(pair<name, phoneNum>(name("T"), phoneNum("555-9999")));
directory.insert(pair<name, phoneNum>(name("C"), phoneNum("555-9678")));
string str;
cout << "Enter name: ";
cin >> str;
multimap<name, phoneNum>::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;
}
Related examples in the same category