You can use std::remove_if
. This will move all remaining elements to the front, and returns an iterator to the new back. You can then erase it:
struct my_predicate
{
bool operator()(HANDLE) const
{
return ...;
}
};
typedef std::vector<HANDLE> vector_type;
vector_type::iterator newEnd =
std::remove_if(myvector.begin(), myvector.end(), my_predicate());
myvector.erase(newEnd, myvector.end());
It's commonly done on one line. If your compiler supports lambda's (C++0x), you can do:
vector_type::iterator newEnd =
std::remove_if(myvector.begin(), myvector.end(), [](HANDLE){ return ... });
myvector.erase(newEnd, myvector.end());
To keep the predicate local.
If you think this is ugly, just wrap it up:
template <typename Vec, typename Pred>
Pred erase_if(Vec& pVec, Pred pPred)
{
pVec.erase(std::remove_if(pVec.begin(), pVec.end(),
pPred), pVec.end());
return pPred;
}
Then:
erase_if(myvector, mypredicate);
C++0x lambda's work the same, of course.