I want to use std::remove_if
with a predicate that is a member function of a differenct calss.
That is
class B;
class A {
bool invalidB( const B& b ) const; // use members of class A to verify that B is invalid
void someMethod() ;
};
Now, implementing A::someMethod
, I have
void A::someMethod() {
std::vector< B > vectorB;
// filling it with elements
// I want to remove_if from vectorB based on predicate A::invalidB
std::remove_if( vectorB.begin(), vectorB.end(), invalidB )
}
Is there a way to do this?
I have already looked into the solution of
Idiomatic C++ for remove_if, but it deals with a slightly different case where the unary predicate of remove_if
is a member of B
and not A
.
Moreover,
I do not have access to BOOST or c++11
Thanks!
Once you're in
remove_if
, you've lost thethis
pointer ofA
. So you'll have to declare a functional object which holds it, something like:Just pass an instance of this to
remove_if
.If you don't want to create additional functors and you're restricted to C++03, use
std::mem_fun_ref
andstd::bind1st
:Alternatively, if your compiler supports TR1, you can use
std::tr1::bind
: