   C++ remove_if pred member function
  I'm trying to use remove_if as recommended by a college.ActorProjectile{//...typedef std::vector< ActorProjectile > Vector} ;bool test( ActorProjectile& projectile ){   return( false ) ;}GameClass{public :   void idle()   { ActorProjectile::Vector::iterator  delStart ; //compiles fine, though untested delStart = std::remove_if( m_playerBullets.begin(),m_playerBullets.end(), test ) ; // does not compile "error C2064: term does not evaluate to a function taking 1 arguments" delStart = std::remove_if( m_playerBullets.begin(),m_playerBullets.end(), &GameClass::testProjectile ) ; m_playerBullets.erase( delStart, m_playerBullets.end() ) ;   }   bool testProjectile( ActorProjectile& projectile )   { //move bullet //if out of range, or hit enemy //return( true ) ; return( false ) ;   }private :   ActorProjectile::Vector  m_playerBullets ;   //ActorEnemy::Vector  m_enemies ;} ;I'm trying to do this in order to avoid about 3 loops that possibly get restarted due to erasing from the vector.I have seen out there on various pages the use of [bind1st] and [mem_fun], but these don't fit this example (I can see why for [mem_fun]).Any way to use a member function as the pred in [remove_if] as in the code shown?Any help would be great.   
  Is there a reason your predicate needs to be a member function?  Technically you can, but it's often not necessary:struct MyPredicate{   MyPredicate(GameClass* gc) gc(gc) {}   bool operator()(const ActorProjectile& ap) const   { return gc->TestProjectile(ap);   }private:   GameClass* gc;};GameClass* game;std::vector<ActorProjectile> vec;vec.erase(std::remove_if(vec.start(), vec.end(), MyPredicate(game)), vec.end());;  The predicate is somewhat illogical because it requires one ActorProjectile to test another ActorProjectile.If that is not the intention, just make it static.If that is the intention, you need to determine where the second ActorProjectile comes from and bind it to the whole thing. E.g (from memory, hopefully works):std::bind1st(std::mem_fun_ref(&ActorProjectile::test), object_to_be_called_on)but in such a situation, boost::bind (goes into C++0x standard and supersedes bind1st and bind2nd) might be simplerboost::bind(&ActorProjectile::test, object_to_be_called_on, boost::_1);//end up calling: object_to_be_called_on.test(_1);where _1 is a placeholder for the value that the remove_if function passes to the generated functor.   ;  @ ZipsterI would say that as each projectile needs to be collision tested with each enemy (the vector of which is a member of the GameClass), I would have thought that a member function was the simplest... (EDIT: How would I achieve the use of the member function without the use of a whole other struct .) But I could be wrong, be it with my use of the function pointer, or with the use of the [remove_if] function. Which brings me to…<br><br>@ visitor<br>It would appear that [remove_if] is not the function for me. I want to perform the given function [bool testProjectile( ActorProjectile& projectile )] &#111;n each projectile, where the return value determines if the given projectile should later be removed from the vector. The function, ideally, should arrange the list so that the entries to be deleted are at the end, and returns an iterator pointing to the start of the entries to be deleted. I understand this is what [remove_if] does, but I didn't realise the predicate compared an entry to another entry…<br><br>What was meant by making "it static"? Make the GameClass::testProjectile function static, or use the method suggested by Zipster?<br><br>Hhmmm, any suggestions?;  Sorry, misread your code a bit (didn't even scroll down) :)Yes, you should be able to use Zipster's method, or use the binders to bind this, instead of another Projectile.std::bind1st(std::mem_fun(&GameClass::testProjectile), this)//orboost::bind(&GameClass::testProjectile, this, boost::_1);Quote:Original post by ZipsterIs there a reason your predicate needs to be a member function?  Technically you can, but it's often not necessary:struct MyPredicate{   MyPredicate(GameClass* gc) gc(gc) {}   bool operator()(const ActorProjectile& ap) const   { return gc->TestProjectile(ap);   }private:   GameClass* gc;};GameClass* game;std::vector<ActorProjectile> vec;vec.erase(std::remove_if(vec.start(), vec.end(), MyPredicate(game)), vec.end());You could also do this in C++0x:GameClass* game;std::vector<ActorProjectile> vec;vec.erase(std::remove_if(vec.start(), vec.end(), [&](GameClass* gc)->bool { return gc->TestProjectile(ap); }), vec.end());As opposed to making a functor class.   ;  Sorry about this, tired and can't figure it out...The following can be test compiled - the errors that occur are in the comments. (MSVC 2005)Should I be using the method Zipster describes due to its ease?#include <vector>#include <functional>#include <algorithm>class ActorProjectile{//...public:typedef std::vector< ActorProjectile > Vector ;} ;bool test( ActorProjectile& projectile ){   return( false ) ;}class GameClass{public :   void idle()   { ActorProjectile::Vector::iterator  delStart ; //compiles fine, though untested delStart = std::remove_if( m_playerBullets.begin(),m_playerBullets.end(), test ) ; // does not compile "error C2529: '_Right' : reference to reference is illegal" //and "error C2535: 'bool std::binder1st<_Fn2>::operator ()(ActorProjectile &(&)) const' : member function already defined or declared" delStart = std::remove_if( m_playerBullets.begin(),m_playerBullets.end(), std::bind1st( std::mem_fun( &GameClass::testProjectile ), this ) ) ; m_playerBullets.erase( delStart, m_playerBullets.end() ) ;   }   bool testProjectile( ActorProjectile& projectile )   { //move bullet //if out of range, or hit enemy //return( true ) ; return( false ) ;   }private :   ActorProjectile::Vector  m_playerBullets ;   //ActorEnemy::Vector  m_enemies ;} ;;Quote:Should I be using the method Zipster describes due to its ease?As you like. The version with boost::bind should work too.Unfortunately, bind1st and bind2nd are not particularly advanced and often fail to work even in slightly non-trivial situations. If I'm not mistaken, they might get somewhat deprecated in C++0x, where you can preferably use std::bind (mostly the same as in boost).   ;  If all you're trying to do is erase from a vector while iterating over it, this should work AFAIK:for (ActorProjectile::Vector::iterator it = m_playerBullets.begin();it != m_playerBullets.end();){    it->move();    if (it->outOfRange() || it->hitEnemy())   it = vector.erase( it );    else   ++it;}I was trying to do this not so long ago and felt that using remove_if was a bit of a hassle so I thought I'd reply in case that's what you needed.;  Beware, erasing from a vector element by element in a manual loop is O(n^2) whereas the erase-remove idiom is O(n).   