问题
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 Band not A.
Moreover,
I do not have access to BOOST or c++11
Thanks!
回答1:
Once you're in remove_if, you've lost the this pointer of
A. So you'll have to declare a functional object which holds
it, something like:
class IsInvalidB
{
A const* myOwner;
public:
IsInvalidB( A const& owner ) : myOwner( owner ) {}
bool operator()( B const& obj )
{
return myOwner->invalidB( obj );
}
}
Just pass an instance of this to remove_if.
回答2:
If you don't want to create additional functors and you're restricted to C++03, use std::mem_fun_ref and std::bind1st:
std::remove_if(vectorB.begin(), vectorB.end(),
std::bind1st(std::mem_fun_ref(&A::invalidB), some_A));
Alternatively, if your compiler supports TR1, you can use std::tr1::bind:
using std::tr1::placeholders::_1;
std::remove_if(vectorB.begin(), vectorB.end(),
std::tr1::bind(&A::invalidB, some_A, _1));
来源:https://stackoverflow.com/questions/16518566/stdremove-if-using-other-class-method