试图在矢量中找到某些东西时出错,不确定如何修复

Error in trying to find something in a Vector, Unsure how to fix

本文关键字:出错 何修复 不确定      更新时间:2023-10-16

我有一个。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;
}