Skip to content
Quiz · Containers and Iterators · lesson 7 of 11

Quiz — erasing in a loop

What is wrong with this loop that removes zeros from v?

C++
#include <vector>

void drop_zeros(std::vector<int>& v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        if (*it == 0) {
            v.erase(it);
        }
    }
}