简体   繁体   English

在find_if中使用谓词

[英]Using predicate in find_if

I have a struct like this. 我有这样的结构。

struct MaxWinPerElementInfo
{
   std::string paytableName;
   std::string elementName;
   long long  elementCredits;
   bool        multiplierRequired;
};

I want to find the element that matches the paytableName . 我想找到与paytableName匹配的元素。 For that I planned to use std::find_if with a predicate function. 为此,我计划将std::find_if与谓词功能一起使用。

I declared the function object within the struct MaxWinPerElementInfo as, 我在struct MaxWinPerElementInfo中将函数对象声明为

bool operator() ( const MaxWinPerElementInfo &elementInfo ) const
{
    return paytableName == elementInfo.paytableName;
}

Now I tried searching for the element by calling, 现在,我尝试通过调用来搜索元素,

std::find_if( elementInfo.begin(), elementInfo.end(), MaxWinPerElementInfo( paytable));

where elementInfo is std::vector< struct MaxWinPerElementInfo > and paytable is std::string . 其中elementInfostd::vector< struct MaxWinPerElementInfo >paytablestd::string

For this I am getting the error, no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const MaxWinPerElementInfo&' 为此,我得到了错误, no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const MaxWinPerElementInfo&'

I cannot use c++11 since this is a legacy code. 我不能使用c ++ 11,因为这是旧版代码。

What I am missing here? 我在这里想念的是什么? Any suggestions would be really helpful. 任何建议都会很有帮助。

Your class has no conversion constructor that accepts an argument of type std::string. 您的类没有可以接受类型为std :: string的参数的转换构造函数。 So the compiler issues an error parsing expression 所以编译器发出一个错误解析表达式

MaxWinPerElementInfo( paytable)

You could use a lambda expression instead of the functional object. 您可以使用lambda表达式代替功能对象。 For example 例如

std::find_if( elementInfo.begin(), elementInfo.end(), 
              [&] ( const MaxWinPerElementInfo &elementInfo  )
              {
                 return paytableName == elementInfo.paytableName;
              } );

If you may not to use lambda expressions then you could define inside the structure a functional object. 如果您可能不使用lambda表达式,则可以在结构内部定义一个功能对象。 For example 例如

struct MaxWinPerElementInfo
{
   std::string paytableName;
   std::string elementName;
   long long  elementCredits;
   bool        multiplierRequired;

   struct FindByPaytableName
   {
      FindByPaytableName( const std::string &paytableName ) : paytableName( paytableName ) {}
      std::string paytableName;
      bool operator() ( const MaxWinPerElementInfo &elementInfo ) const
      {
          return paytableName == elementInfo.paytableName;
      }
   };
};


std::find_if( elementInfo.begin(), elementInfo.end(), 
              MaxWinPerElementInfo::FindByPaytableName( paytableName ) );

Using an inner structure allows you to define several criteria for searching by using various data members of the outer structure. 使用内部结构可以使您通过使用外部结构的各种数据成员来定义多个搜索条件。

std::find_if( elementInfo.begin(), elementInfo.end(), 
    MaxWinPerElementInfo( paytable));

You are trying to construct a MaxWinPerElementInfo by passing it a string. 您正在尝试通过传递一个字符串来构造MaxWinPerElementInfo Do you have a constructor taking a string? 您是否有接受字符串的构造函数? I guess not so the compiler tries to call copy constructor which takes a const MaxWinPerElementInfo reference. 我想不是,因此编译器尝试调用采用const MaxWinPerElementInfo引用的副本构造函数。 If no constructor taking a single argument is defined, compiler will revert to the only one it knows: Copy contructor. 如果未定义带有单个参数的构造函数,则编译器将还原为它知道的唯一一个:复制构造函数。

Since you have a struct and members are public, I would suggest implementing this outside of the class with a functor object. 由于您有一个结构且成员是公共的,因此建议您在类外部使用functor对象实现此功能。

struct MaxWinPerElementInfoPayTablePredicate
{
    MaxWinPerElementInfoPayTablePredicate(const std::string& _paytableName) : 
        paytableName(_paytableName) {}

    bool operator() ( const MaxWinPerElementInfo &elementInfo ) const
    {
        return paytableName == elementInfo.paytableName;
    }

    std::string paytableName;
}

Then call it like so: 然后这样称呼它:

std::find_if( elementInfo.begin(), elementInfo.end(), 
    MaxWinPerElementInfoPayTablePredicate(paytable) );

You need to add a constructor which takes a string and puts it in paytableName. 您需要添加一个接受字符串并将其放入paytableName的构造函数。 Otherwise you cannot create a MaxWinPerElementInfo from a string as you are trying to do within the find_if() call. 否则,您将无法像尝试在find_if()调用中那样从字符串创建MaxWinPerElementInfo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM