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?