I simply wanna erase the specified element in the range-based loop:
vector vec = { 3, 4, 5, 6, 7, 8 };
for (auto & i:vec)
{
if (i>5)
ve
Removing elements from a vector that you're iterating over is generally a bad idea. In your case you're most likely skipping the 7. A much better way would be using std::remove_if for it:
vec.erase(std::remove_if(vec.begin(), vec.end(),
[](const int& i){ return i > 5; }),
vec.end());
std::remove
shift the elements that should be removed to the end of the container and returns an iterator to the first of those elements. You only got to erase those elements up to the end then.