请教一个二元谓词问题...........
- C/C++ code
#include <set>#include <string>#include <iostream>using namespace std;enum MenuOptionSelection{ InsertContactsetEntry = 0, DisplayEntries = 1, FindNumber = 2, EraseEntry = 3, FindName = 4, QuitApplication = 5};struct ContactItem{ string strContactsName; string strPhoneNumber; ContactItem(const string &strName,const string &strNumber) { strContactsName = strName; strPhoneNumber = strNumber; } bool operator== (const ContactItem &itemToCompare) const { return (itemToCompare.strContactsName == this->strContactsName); } bool operator< (const ContactItem &itemToCompare) const { return (this->strContactsName < itemToCompare.strContactsName); }};struct FindContactGivenNumber{ bool operator() (const ContactItem &item1,const ContactItem &item2) const { return (item1.strPhoneNumber < item2.strPhoneNumber); }};int ShowMenu(){ cout<<"***What would you like to do next?***"<<endl<<endl; cout<<"======================================="<<endl; cout<<"Enter 0 to feed a name and phone number"<<endl; cout<<"Enter 1 to Display all entries"<<endl; cout<<"Enter 2 to find an entry"<<endl; cout<<"Enter 3 to erase an entry"<<endl; cout<<"Enter 4 to find name with phone number"<<endl; cout<<"Enter 5 to quit this application"<<endl; cout<<"======================================="<<endl<<endl; cout<<"> "; int nOptionSelected; cin>>nOptionSelected; cout<<endl; return nOptionSelected;}ContactItem GetContactInfo(){ cout<<"***Feed contact information***"<<endl; string strName; cout<<"Please enter the person's name"<<endl; cout<<"> "; cin>>strName; string strPhoneNumber; cout<<"Please enter "<<strName<<"'s phone number"<<endl; cout<<"> "; cin>>strPhoneNumber; return ContactItem(strName,strPhoneNumber);}void DisplayContactset(const set<ContactItem> &setContacts){ cout<<"***Displaying contact information***"<<endl; cout<<"There are "<<setContacts.size()<<" entries:"<<endl; set<ContactItem>::const_iterator iContact; for (iContact = setContacts.begin() ;iContact != setContacts.end() ;++iContact) { cout<<"Name: '"<<iContact->strContactsName; cout<<"' Number: '"<<iContact->strPhoneNumber<<"'"; cout<<endl; } cout<<endl;}void FindContactNumber(const set<ContactItem> &setContacts){ cout<<"***Find a contact***"<<endl; cout<<"Whose number do you wish to find?"<<endl; cout<<"> "; string strName; cin>>strName; set<ContactItem>::const_iterator iContactFound = setContacts.find(ContactItem(strName,"")); if (iContactFound != setContacts.end()) { cout<<strName<<" is reachable at number: "; cout<<iContactFound->strPhoneNumber<<endl; } else { cout<<strName<<" was not found in the contacts list"<<endl; } cout<<endl;}void FindContactName(const set<ContactItem> &setContacts){ cout<<"***Find a contact***"<<endl; cout<<"Whose name do you wish to find?"<<endl; cout<<"> "; string strNumber; cin>>strNumber; set<ContactItem>::const_iterator iContactFound = setContacts.find(ContactItem("",strNumber)); if (iContactFound != setContacts.end()) { cout<<strNumber<<" is reachable at number: "; cout<<iContactFound->strContactsName<<endl; } else { cout<<strNumber<<" was not found in the contacts list"<<endl; } cout<<endl;}void EraseContact(set<ContactItem> &setContacts){ cout<<"***Erase a contact***"<<endl; cout<<"Whose number do you wish to erase?"<<endl; cout<<"> "; string strName; cin>>strName; size_t nErased = setContacts.erase(ContactItem(strName,"")); if (nErased > 0) cout<<strName<<"'s contact information erased."<<endl; else cout<<strName<<" was not found!"<<endl; cout<<endl;}int main(){ set<ContactItem> setContacts; int nUserSelection; string strInput; while ((nUserSelection = ShowMenu()) != (int)QuitApplication) { switch (nUserSelection) { case InsertContactsetEntry: setContacts.insert(GetContactInfo()); cout<<"Contacts set updated!"<<endl<<endl; break; case DisplayEntries: DisplayContactset(setContacts); break; case FindNumber: FindContactNumber(setContacts); break; case EraseEntry: EraseContact(setContacts); DisplayContactset(setContacts); break; case FindName: FindContactName(setContacts); break; case QuitApplication: break; default: cout<<"Invalid input '"<<nUserSelection; cout<<"'. Please choose an option between 0 and 4"<<endl<<endl; break; } } cout<<"Quiting! Bye!"<<endl<<endl; char response; cin>>response; return 0;}
这是一个作业:
在不修改ContactItem的情况下,扩展本章中的电话薄应用程序使其能够根据电话号码查找人名
书上给了一个提示:定义set时指定一个二元谓词.而这个二元谓词就是struct FindContactGivenNumber部分
其中,FindContactName()函数是我自己添加的,目的是通过这个函数输入号码找到人名
最开始我只添加了FindContactName()函数,我存入了正确的电话号码和人名 输入正确的号码却始终提示不在列表中
这个问题我不知道怎么解释,为什么存进去了缺找不到
第二个问题就是这个二元谓词到底怎样使用,我试过很多方法 ,编译器全部都报错 我想我肯定没加对
[解决办法]
在这个函数中void FindContactName(const set<ContactItem> &setContacts),
下面这一个语句有错:
set<ContactItem>::const_iterator iContactFound = setContacts.find(Contac tItem("",strNumber));
set容器中查找的是ContactItem 对象的整体,而不是只查strNumber这一个变量