I'm coding in C++. I have a project with so many files. I have a vector of pairs named list as follows:
std::vector< std::pair< structure1, double> > list;
and I want to check if for a specific double value z
, there exists an element: el
in the list such that: el.second == z
I want to use find_if
To do so , I've implemented a method : Scheduled
that takes two arguments: the first one is an element like those stored in the list , the second one is the specific value to look for.
I tried several ways but I end up getting an error always
1st way:
bool classA::Scheduled(std::pair< structure1,double > const el, double const t )
{
return el.second==t;
}
inside another method but still in the same class: classA
auto Scheduled1 = std::bind(&classA::Scheduled,this,_1,z);
bool call=std::find_if(list.begin(),list.end(),Scheduled1)=!list.end();
This solution gives the following error:
error: ‘Scheduled1’ does not name a type
2nd way: directly using lambda
bool call = std::find_if(list.begin(),list.end(),[this](std::pair<struct1,double> const& el){return el.second==z;})!=list.end();
z is a member variable of classA This second way of coding gives rise to this error:
error: no matching function for call to
‘find_if(std::vector >::iterator, std::vector >::iterator, classA::method1(int)::__lambda0)’
There's no need to mix
bind
,bind1st
andmem_fun
to do this (the latter two are deprecated in C++11); just use a lambdaOr if you want to call
Scheduled
If you must use
bind
In either case, you might want to change
Scheduled
to be astatic
member function since it doesn't need access to any non-static members, in which case thebind
option becomesAlso,
Scheduled
should probably take thestd::pair
argument byconst&
to avoid unnecessary copies.Another option is to use
any_of
instead offind_if
, which avoids having to compare the result with the end interatorHere's an explanation of what's wrong with your attempt.
Scheduled
is a non-static member function, which means it takes an implicit first argument, a pointer to the instance it must be invoked on, i.e. thethis
pointer. Moreover, the syntax for creating a pointer to member function is&ClassName::MemberFunctionName
. So the above line should bebind
returns a function object of unspecified type, but you use that object as if it were a member function (mem_fun(&classA::Scheduled1)
) which is clearly incorrect. Simply passing the aboveScheduled1
object as the 3rd argument tofind_if
in your example should work.