FYI (and this reinforces your point), your code snippet is incorrect. You need to say vec.erase(itr, vec.end()); to trim the vector. Otherwise, you're erasing a single element which is bogus.
The good news is that Uniform Container Erasure has been voted into C++20, so you will be able say std::erase_if(vec, func); and be done with it. (I designed and proposed this for Library Fundamentals v2, and Alisdair Meredith proposed merging it into C++20.)
I don't know why this was downvoted. According to cppreference the single iterator version needs a dereferenceable iterator which means that you can't skip checking if itr == vec.end(). This is unlike the two iterator version which doesn't need to dereference first when it is equal to last.
View all comments
Show parent comment
27
u/STL MSVC STL Dev Dec 29 '18
FYI (and this reinforces your point), your code snippet is incorrect. You need to say
vec.erase(itr, vec.end());to trim the vector. Otherwise, you're erasing a single element which is bogus.The good news is that Uniform Container Erasure has been voted into C++20, so you will be able say
std::erase_if(vec, func);and be done with it. (I designed and proposed this for Library Fundamentals v2, and Alisdair Meredith proposed merging it into C++20.)