我有一个简单的电话簿,但我想修改它,以便我可以搜索姓氏的列表,它应该返回具有相同姓氏的名字。一个人可以有多个号码,一个号码可以属于不止一个人(家庭成员)。一般情况下,电话号码可以包含非数字字符,例如,如果我有一个如下所示的姓名列表
john williams
smart williams
hendrix james
drey williams
如果我输入 williams 或搜索 williams 它应该返回
john williams
smart willams
drey williams
我想用Map<String,List<String>>.
class name {
string str;
public:
name() {
str = "";
}
name(string s) {
str = s;
}
string get() {
return str;
}
};
// Define less than relative to name objects.
bool operator<(name a, name b){
return a.get() < b.get();
}
class surname {
string str;
public:
surname() {
str = "";
}
surname(string s) {
str = s;
}
string get() {
return str;
}
};
int main()
{
map<name, surname> directory;
directory.insert(pair<name, surname>(name("James"), surname("williams")));
directory.insert(pair<name, surname>(name("Coz"), surname("williams")));
directory.insert(pair<name, surname>(name("James"), surname("trues")));
directory.insert(pair<name, surname>(name("Tni"),surname("mutton")));
// given a name, find number
string str;
cout << "Enter name: ";
cin >> str;
map<name, surname>::iterator p;
p = directory.find(name(str));
if(p != directory.end())
cout << "surname: " << p->second.get();
else
cout << "Name not in directory.\n";
system("pause");
return 0;
}
提前致谢