Quantcast
Channel: Erase some of a vector's elements in a for-each loop without iterating the whole vector - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Erase some of a vector's elements in a for-each loop without iterating the whole vector

$
0
0

I have a vector and I am searching for an element in it while iterating over the vector with a for-each loop. If I find any invalid elements during the search, I would like to remove them from the vector.

Basically, I want to do something like this:

for (auto el : vec) {    if (el == whatImLookingFor) {        return el;    } else if (isInvalid(el)) {        vec.erase(el);    }}

I looked at some other questions like this and this, but both recommend using std::remove_if. That will iterate over the whole vector and remove all invalid elements, instead of iterating only until I find the element I'm looking for, and ignoring any elements after that.

What would be a good way to do this?


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images