为什么抛出这个断言的向量失败警告?

ric*_*d.g 1 c++ vector assertion

我在调试模式下运行我的代码,程序弹出一个断言失败的消息.请帮我找出导致此断言失败的部分.:{

Debug Assertion Failed!
Expression: vector iterators incompatible
Run Code Online (Sandbox Code Playgroud)

码:

int main()
{
vector<int> a(5);
fill(a.begin(), a.end(), 5);
a[2] = 3;
a[1] = 2;  //so now a = {5,2,3,5,5} 

auto it = a.begin();
for (; it != a.end();)
{
    if (*it == 5)
        a.erase(it);  //Remove 5
    else
        it++;
}
copy(a.begin(), a.end(), ostream_iterator<int>(cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)

小智 5

在迭代期间擦除时,擦除的迭代器变为(并且其他可能变为)无效:

做了:

它= a.erase(it);