views:

176

answers:

4

I'm basically looping through all the entries to check whether some entries is to be erased, but seems in a wrong way:

std::vector<HANDLE> myvector; 
for(unsigned int i = 0; i < myvector.size(); i++)
{
    if(...)
         myvector.erase(myvector.begin()+i);
}

Anyone spot the problem in it? How to do it correctly?

+7  A: 

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.

GMan
A: 

You should use vector iterator for that, AND, on deletion, assign the iterator the value returned by erase()

for (it = myvector.begin(); it != myvector.end();) {
    if (...) {
        it = myvector.erase(it);
        continue;
    }

    ++it;
}
Ivan Krechetov
This seems to be the easiest solution,but I'm not sure whether it really works?
I guess the one after the erased entry will be skipped by `it++`
this is slightly broken, you need to not do thr `it++` when the if is true (otherwise you can skip over an element or worse yet, you can skip over `end()`. Better would be this: `for (it = v.begin(); it != v.end();) { if(...) it = v.erase(it); else ++it; }`
Evan Teran
It is also worth noting that this is algorithmically terrible, because `erase` will cause all of the following elements to be "moved down one" the remove_if/erase idiom is much better because it avoids this.
Evan Teran
Yep, all true. Thanks for your comments. Will add "continue" now, and move the increment down.
Ivan Krechetov
+4  A: 

Your problem is algorithmic. What happens if two adjacent elements meet your criterion for deletion? The first will be deleted, but because i is incremented after each iteration of the loop, the second will be skipped. This is because a vector is contiguous in memory, and all elements after the deleted one are moved forwards one index.

An ugly hack would be to do the following:

std::vector<HANDLE> myvector; 
for(unsigned int i = 0; i < myvector.size();)
{
    if(...)
         myvector.erase(myvector.begin()+i);
    else
         i++;
}

I'm not sure if using iterators would work, because calling erase invalidates iterators to elements after the erased element.

The elegant solution would be to use std::remove_if, as GMan suggested. This would abstract away two things:

  1. Your removal condition
  2. The process by which the elements of a container are removed

Edit: I should also add, the hacked solution is O(n2) in the worst case. GMan's solution is O(n), assuming your removal condition is O(1). I would strongly encourage you to learn and use GMan's solution.

kevlar
can also replace the `if(...)` with `while(...)`, as long as the condition can also tell when it's past the end of the vector.
goldPseudo
I don't quite understand GMan's solution,so I accept this hack version.
@user: You should ask for clarification and/or get [a book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list) to teach you C++.
GMan
I've always just looped over the vector backwards in the past...
Mark
btw: `erase()` returns a new (valid) iterator to the next element after the erased one.
sth
@sth I see, you're right. Ivan's edited solution uses that fact.
kevlar
+1  A: 

Perhaps another hacky solution...

std::vector<HANDLE> myvector; 
for(unsigned int i = myvector.size()-1; i >=0; --i)
{
    if(...)
         myvector.erase(myvector.begin()+i);
}

But at least it's simple.

Mark