Predicate syntax when splitting a vector of pointers (C ++)

I have a vector of pointers to objects. I would like to remove objects from this vector according to the attribute reported by the member function.

I am trying to follow a good example I found on how to remove certain pointers (and related objects) from a vector. The basic idea is to split the vector, remove the selected objects, and then remove the pointers to those objects. Below is an example (from Dr. Dobbs ):

vector<Object *> v ;

v.push_back( new Object( ... ) ) ;
...
vector<Object *>::iterator last =
partition( v.begin(), v.end(), not1(predicate()) ) ;
for( vector<Object *>::iterator i = last ; i != v.end() ; ++i )
{
  delete *i ;
}
v.erase( last, v.end() ) ;

      

I am exaggerated in the correct syntax for the predicate. My objects are of the Strain class and my vector is vector <Strain *> liveStrains. The predicate must be the Strain member function isExtinct (). The following doesn't work:

 vector< Strain * >::iterator last = partition( liveStrains.begin(), liveStrains.end(), not1(std::mem_fun_ref(&Strain::isExtinct)) );

      

I see that I am trying to call a member function on a pointer to an object, not the object itself. To get around this tried changing and to * (I'm obviously a newbie) and I tried to create a member function for the Simulation class that updates the liveStrains in the member function. I'm not sure it's worth looking into the details of what didn't work. I am severely confused about the syntax options available, or if what I am trying to do is even allowed.

Thanks in advance for your help.

+2


source to share


1 answer


The solution is to use std :: mem_fun , which is done for pointers to objects, not std :: mem_fun_ref.



+1


source







All Articles