【发布时间】:2013-12-22 12:38:42
【问题描述】:
我在调试模式下运行我的代码,程序弹出断言失败消息。请帮我找出导致此断言失败的部分。 :{
Debug Assertion Failed!
Expression: vector iterators incompatible
代码:
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"));
}
【问题讨论】:
-
顺便说一句,
std::remove已经这样做了,如果你使用擦除删除成语。