All challenges
Code AnalysisIntermediate 60s

Iterator invalidation while erasing in a vector loop

What is wrong with this loop?
Starter code
#include <vector>

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

Think you can answer under pressure?

Get matched and answer live questions in 3-minute 1v1 battles. Elo ranking, badge tiers and bragging rights on the line.

Play this in a 3-minute battle