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

时间:2015-05-16 18:05:55

标签: c++ oop vector

所以我有一个.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.

任何帮助表示赞赏!谢谢。

3 个答案:

答案 0 :(得分:1)

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

您可以使用其他搜索方法(例如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;
}

当然还有其他方法。

无论如何,祝你好运。

答案 1 :(得分:0)

您需要加入:#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 =&#34; yop&#34;;
  • vector [0] .disc = undifined_value;
  • vector [1] .name =&#34;&#34;;
  • vector [1] .disc = 42.42;

更正后,您将拥有:

  • vector [0] .name =&#34; yop&#34;;
  • vector [0] .disc = 42.42;

要在结构上使用函数std :: find,还必须添加此函数:

bool operator==(const customer &c, const string &name){
return (c.name == name);
}

std :: find将调用此函数来检查值。

答案 2 :(得分:0)

您只需要使用运算符重载,因为您正在使用具有自定义类型的向量,但是向量中的方法std :: find()只能比较基本类型中的值。尝试以下功能:

bool operator ==(const customer_list& other) const{
  return other.name==this->name;
}