使用 find() 通过 std::set 的迭代器将不起作用。出了什么问题?



我正在尝试创建一个具有联系人列表的程序,用户可以在其中根据电话号码搜索联系人的姓名。 很抱歉包含这么多代码,但这是理解我的问题所必需的:

#include <iostream>
#include <string>
#include <set>
using namespace std;
struct ContactItem
{
string name;
string phoneNumber;
string displayAs;
ContactItem(const string inName, const string inNumber) : name(inName), phoneNumber(inNumber)
{
displayAs = name + ": " + phoneNumber;
}
bool operator== (const ContactItem& searchParameter) const
{
return (this->phoneNumber == searchParameter.phoneNumber);
}
bool operator< (const ContactItem& compareResult) const
{
return (this->name < compareResult.name);
}
operator const char*() const
{
return displayAs.c_str();
}
};
int main()
{
//Initialize a set and populate it with contacts of type ContactItem
set<ContactItem> contactBook;
contactBook.insert(ContactItem("Sally", "123654864"));
contactBook.insert(ContactItem("Joe", "8435102654"));
contactBook.insert(ContactItem("Steve", "8135691234"));
contactBook.insert(ContactItem("Alice", "8432489425"));
//Search for a contact's name by only being given their number
cout << "Please give the number of one contact whose name you would like to know: " << endl;
string userNumber;
getline(cin, userNumber);
auto findNumber = contactBook.find(ContactItem("", userNumber));
if (findNumber != contactBook.end())
cout << "The name of the contact whose number matches the phone number given is: " << (*findNumber).name << endl;
else
cout << "Contact not found" << endl;
return 0;
}

我的问题似乎总是与线路auto findNumber = contactBook.find(userNumber);有关。 每次我运行此代码时,都会显示消息"找不到联系人"。 我不知道我做错了什么。 这是我对operator==的重新定义吗?

上面的代码受到Rao,Siddhartha的启发。山姆每天在一小时内自学C++。第 8 版,印第安纳州印第安纳波利斯,山姆斯,2017 年。

您不想使用std::set::find来执行此操作。std::set::find是找到完全匹配,但您正在寻找部分匹配。std::set::find将只查看联系人的子集,因为它知道它们已排序。 但是您需要检查所有联系人,因为它们中的任何一个都可以与电话号码匹配。

您需要的是std::find_if<algorithm>.std::find_if接受谓词,谓词是一个函数或类似函数的对象,可以判断这是否是正确的。

首先,包括<algorithm>

#include <algorithm>

我们可以对谓词使用 lambda:

auto findNumber =
std::find_if(contactBook.begin(), contactBook.end(),
[&userNumber](const ContactItem &contact) {
return contact.phoneNumber == userNumber;
});

如果您以前没有使用过 lambda,这看起来很奇怪。 lambda 就像一个带有状态的无名函数。

方括号[]告诉编译器这是一个 lambda。&userNumber说,在 lambda 的主体中,我们需要引用当前范围内的userNumber变量。 (这称为"按引用捕获"。

括号括起类似函数的参数列表。std::find_if将通过传入对联系人的引用,在每个联系人上调用此 lambda,就好像它是一个常规函数一样。

lambda 的主体(在大括号中{}中(是一个函数体,它返回一个bool来告诉用户传入的联系人是否符合我们的匹配标准。 主体可以引用传入的参数以及从定义范围"捕获"的任何内容。 在这种情况下,我们只关心联系人的电话号码是否与所需的电话号码匹配。

std::set文档中所述,默认情况下,它使用std::less,在这种情况下,它使用您提交ContactItem::operator<。当您按字段比较结构时name- 此字段用于比较元素,因此基本上您正在尝试查找具有空名称的联系人。您需要为此std::set指定不同的比较器,或者相应地更改ContactItem::operator <

注意:与std::unordered_setstd::set不同,它不使用任何形式的operator==,正如您在documentatioon中看到的那样:

std::set 是一个关联容器,其中包含一组排序的 键类型的唯一对象。使用键比较完成排序 函数比较。

std::less文件说:

用于执行比较的函数对象。除非专业, 在类型 T 上调用运算符<。

这里的问题是std::set不使用您的operator ==来查找元素。 相反,它使用与比较对象相同的东西,即operator <。 您要么必须更改operator <以按phoneNumber进行比较,要么提供指定的联系人。

你也可以考虑一个提升多索引容器,让你索引我的名字和号码。

相关内容

最新更新