Often, we'd like to apply the erase remove idiom to properly eradicate an element from a vector e.g.:
v.erase( std::remove( std::begin(v), std::end(v), 5 ), std::end(v) );
where v is a vector of integers.
But what if the vector were sorted first? Will the following do the same thing, and if so, is it the most optimal way?
std::sort(v.begin(), v.end());
IntegerVector::iterator it = std::lower_bound(v.begin(), v.end(), 5);
if(it != v.end()) {
(void)v.erase(it, v.end());
}
Does this correctly apply the erase-remove idiom? Is the vector still sorted after the removal process (I assume that it is)? Don't we need to do something like this:
(void)v.erase(std::remove(it), v.end());
(incorrect syntax for std::remove, but hopefully you get the idea).
Thanks,
Ben.
(void)v.erase(...);
;v.erase(...);
will do the same. – user2249683