搜索结构向量 [英] Search vector of structs
问题描述
我试图在结构向量中找到一个字符串.但是我一直收到错误消息,错误消息太长,所以我将其上传到外部.其中一部分是
I'm trying to find a string within in a vector of structs. But I keep getting an error, the error message is way too long so Ill upload it externally. A part of it is
"错误:'operator==' 不匹配(操作数类型为 'char' 和 'const std::__cxx11::basic_string'){ 返回 *__it == _M_value;}"
"error: no match for 'operator==' (operand types are 'char' and 'const std::__cxx11::basic_string') { return *__it == _M_value; }"
我目前使用的代码是:
struct constants {
std::string name, lastname;
float salary=0;
};
void searchPerson(const std::vector<constants> &search) {
using namespace std;
vector<constants>name;
string searchTerm;
cout << "Enter last name of the person you wish you lookup." << "\n";
cin >> searchTerm;
for (size_t k=0; k<name.size(); k++){
if (std::find(name[k].lastname.begin(), name[k].lastname.end(), searchTerm) != name[k].lastname.end())
{
cout << "Test" << "\n";
}
}
}
我似乎无法让它工作,我不知道出了什么问题.最终目标是让用户输入一个人的姓氏,如果程序中存储了一个姓氏的人,它将打印该人的所有信息(名字、姓氏和薪水).我可能还会使用相同的搜索技术从程序中删除一个人.
I just cant seem to get it working, I don't know whats wrong. The end goal is to let the user input the last name of a person, if theres a person with that last name stored in the program, it will print all the information about that person (first name, last name and salary). I will probably also be using the same search technique to delete a person from the program.
我可以通过简单地使用 for 循环轻松地使其工作,但使用 find 语法肯定是有原因的,下面的代码片段很神奇.
I can easily get it working by simply using a for loop, but there should definitely be a reason for using the find syntax, The below snippet work wonders.
for (size_t i=0; i<name.size(); i++) {
if (name[i].lastname== searchTerm)
cout << "Test" << "\n";
}
推荐答案
没有按照您的想法去做.这是一个 for 循环,它迭代 constants
的向量,并尝试在每个 constants
对象的 lastname
内进行搜索.我认为你不是故意这样做的.您可以像这样简单地将 lastname
与 searchTerm
进行比较:
The isn't doing what you think it is. It's a for loop that iterates a vector of constants
and tries to search within lastname
of each individual constants
object. I don't think you mean to do this. You can simply compare the lastname
with searchTerm
like this:
for (size_t k = 0; k < name.size(); k++) {
if (name[k].lastname == searchTerm)
{
std::cout << "Test" << "\n";
break;
}
}
但是,最好避免使用像 std::vector
这样的 stl 集合来手工制作 for 循环.您可以使用 std::find
但这会在您的结构中使用 operator ==
.您要么必须提供,要么可以使用 std::find_if
代替并提供一个谓词函数来进行比较:
However, hand-crafted for loops are best avoided with stl collections like std::vector
. You could use std::find
but that will make use of an operator ==
within your struct. You will either have to provide or you could use std::find_if
instead and supply a predicate function to do the comparison:
if (std::find_if(name.cbegin(), name.cend(),
[&searchTerm](const constants& c) { return searchTerm == c.lastname; }) != name.end())
{
std::cout << "Test" << "\n";
}
这将避免使用 for 循环.请注意,lambda 函数是谓词,它通过引用捕获您的 searchTerm
并将其仅与 lastname
进行比较.
This will avoid the use of a for loop. Note that a lambda function is the predicate, which captures your searchTerm
by reference and compares it with lastname
only.
这篇关于搜索结构向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!