简体   繁体   中英

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 . For that I planned to use std::find_if with a predicate function.

I declared the function object within the struct MaxWinPerElementInfo as,

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 .

For this I am getting the error, 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.

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. So the compiler issues an error parsing expression

MaxWinPerElementInfo( paytable)

You could use a lambda expression instead of the functional object. 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. 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. 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. 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.

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. Otherwise you cannot create a MaxWinPerElementInfo from a string as you are trying to do within the find_if() call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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