gpt4 book ai didi

c++ - 尝试在 Vector 中查找内容时出错,不确定如何修复

转载 作者:行者123 更新时间:2023-11-28 06:16:08 29 4
gpt4 key购买 nike

所以我有一个 .txt 文件,我正在将其推送到一个 vector 中,并且一切正常。我正在尝试获取用户输入(字符串),并在 vector 中找到匹配项。到目前为止,这是我的代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <list>

using namespace std;

struct customer{
string name;
float disc;
};

class customer_list {
vector<customer> custlist;

public:
void load(string fname){
ifstream list;
list.open ("customers.txt");
customer c;
double val = 0;
if (list.is_open()){
while (! list.eof() ){
list >> c.name;
custlist.push_back(c);
list >> c.disc;
custlist.push_back(c);
val++;
}
}
list.close();
int val2 = custlist.size()/val;

for (int j=0; j < custlist.size() - 2; j+= val2){
cout << " Name: " << custlist[j].name << endl;
cout << " Discount: " << custlist[j+1].disc << endl;
}
}


bool in_list(string & query){ //return true if query in list, false otherwise
if (find (custlist.begin(), custlist.end(), query) !=custlist.end()){
cout << "Welcome back," << query << "." <<endl;
return true;
}else{
cout << "No Matches found." << endl;
return false;
}
}
};

int main (){

customer_list a;
string name;

a.load("customers.txt");

cout << "What is your name?" << endl;
cin >> name;

a.in_list(name);
}

运行时出现这个错误:

In file included from prt2.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:865:22: error: invalid operands
to binary expression ('customer' and 'const std::__1::basic_string<char>')
if (*__first == __value_)
~~~~~~~~ ^ ~~~~~~~~
prt2.cpp:46:15: note: in instantiation of function template specialization 'std::__1::find<std::__1::__wrap_iter<customer *>,
std::__1::basic_string<char> >' requested here
if (find (custlist.begin(), custlist.end(), query) !=custlist.end()){
^
1 error generated.

感谢任何帮助!谢谢。

最佳答案

您的查找 正在将苹果与橙子进行比较。使用 std::find 寻找的搜索值必须与序列的类型相同或可转换为序列的类型,这并非严格正确,但通常是实践中的。您可以使用比较两个值的等价运算符 (operator ==) 来避免这种情况,即使它们是不同的类型,但实际上不需要这种疯狂。无论如何,您要在充满 customer 的丛林中搜索 std::string

您可以使用其他搜索方法来解决这个问题,例如 std::find_if 和自定义限定 lamda 表达式:

像这样:

bool in_list(const string & s)
{
if (std::find_if(custlist.begin(), custlist.end(),
[&s](const customer& c) { return c.name == s; }) != custlist.end())
{
cout << "Welcome back," << s << "." <<endl;
return true;
}

cout << "No Matches found." << endl;
return false;
}

当然还有其他方法。

无论如何,祝你好运。

关于c++ - 尝试在 Vector 中查找内容时出错,不确定如何修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30279060/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com