First off : The point of a vector is to know the order (otherwize use a set or list etc) .
Second : if you have a vector of pointer either :
(a) consider an autoptr/boost::shared_ptr [prefered] and just delete the element with a remove_if and then an erase e.g
std::vector<boost::shared_ptr<Obj> > Vec;
// ... populate etc
std::vector<boost::shared_ptr<Obj> >::iterator ec=
remove_if(Vec.begin(),Vec.end(),
boost::bind(&Vec:requiresDel,_1));
Vec.erase(ec,Vec.end());
Now you might have a deletion that is more complex or depends on the index number. then try this:
std::vector<Obj*> Vec;
// ... populate here.
for(long int i=Vec.size()-1;i>=0;i--)
{
if ( delRequied(Vec[i]) )
{
delete Vec[i];
Vec.erase(Vec.begin()+i);
}
}
Note:
Normally I use size_t not long int but that has lead to confusion in the past.
StuXYZ
Practically a Master Poster
681 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
Skill Endorsements: 0