试图在矢量中找到某些东西时出错,不确定如何修复
Error in trying to find something in a Vector, Unsure how to fix
我有一个。txt文件,我把它压入一个向量,这是正常的。我试图采取用户输入(字符串),并在向量中找到匹配。这是我到目前为止的代码:
#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.
感谢任何帮助!谢谢。
你的find
正在比较苹果和橙子。用std::find
寻找的搜索值必须与序列的类型相同或可转换为序列的类型,这不是严格正确的,但通常是这样做的。您可以使用比较两个值的等价操作符(operator ==
)来规避这个问题,即使它们是不同的类型,但实际上不需要这种疯狂。无论如何,你是在一个充满customer
的丛林中寻找std::string
。
您可以通过使用其他搜索方法(如std::find_if
和自定义限定lambda表达式)来绕过该问题:
:
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;
}
当然还有其他的方法。
祝你好运。
你需要包括:#include <algorithm>
正确使用std::find
它会起作用的。
您还可以改进这部分代码:
if (list.is_open()){
while (! list.eof() ){
list >> c.name;
custlist.push_back(c);
list >> c.disc;
custlist.push_back(c);
val++;
}}
if (list.is_open()){
while (! list.eof() ){
list >> c.name;
list >> c.disc;
custlist.push_back(c);
val++;
}}
,因为你使用向量的2个元素来存储一个元素,例如,如果你有yop 42.42,你将在你的向量:
- vector[0].name = "yop";
- 向量[0]。
- vector[1].name = ";
- 向量[1]。
修正后,你将得到:
- vector[0].name = "yop";
- 向量[0]。
要在结构体上使用std::find函数,还必须添加这个函数:
bool operator==(const customer &c, const string &name){
return (c.name == name);
}
std::find将调用这个函数来检查值
您只需要使用操作符重载,因为您使用的是自定义类型的vector,但是vector中的std::find()方法只能比较基本类型中的值。试试下面的函数:
bool operator ==(const customer_list& other) const{
return other.name==this->name;
}
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- Boost Graph Library,修复节点大小
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 如何修复函数样式强制转换或类型构造的预期"("?
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 当使用带有VS2019或VSCode的虚幻引擎4.24.2时,我如何修复这些错误的Intellisense错误
- 分段错误当我试图运行程序时出错
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 如何修复使用变量初始化数组时出错
- 试图在矢量中找到某些东西时出错,不确定如何修复