我有一个结构,其中数据定义为:
typedef struct contacts
{
string name; //{jhonathan , anderson , felicia}
string nickName; //{jhonny , andy , felic}
string phoneNumber; // {13453514 ,148039 , 328490}
string carrier; // {atandt , coolmobiles , atandt }
string address; // {1bcd , gfhs ,jhtd }
} contactDetails;
vector <contactDetails> proContactFile;
在这里我想对name
进行二进制搜索。如果搜索到的姓名可用,那么我想显示该姓名的相关联系信息(nickname,phone number ,carrier ,address
)。如何执行此操作?
答案 0 :(得分:0)
auto cmpFn = [](const contacts &c1, const contacts &c2) {return c1.name < c2.name;};
// 1. vector must be sorted
std::sort(proContactFile.begin(), proContactFile.end(), cmpFn);
// 2. what to find
contacts findme; findme.name = "cat";
// 3. find
auto it = std::lower_bound(proContactFile.begin(), proContactFile.end(), findme, cmpFn);
bool found = it != proContactFile.end() && it->name == findme.name;