Why does the following code produce an assertion error: Expression: list iterators incompatible
?
#include <list>
using namespace std;
int main()
{
list<int> a;
a.push_back(1);
list<int>::iterator iter=a.begin();
a.erase(iter);
iter==a.end();
}
What you want to do is this:
#include <list>
using namespace std;
int main()
{
list<int> a;
a.push_back(1);
list<int>::iterator iter=a.begin();
iter = a.erase(iter);
}
在一个给定集合的迭代a
时变为无效a
变化,例如,通过移除元素。
When erasing iter
it got invalidated. I think invalid iterator can't be used for anything other than assigning to them, not even comparing them with anything. You might have wanted to use
iter = a.end();
the iterator is invalid after the erase. in your case erase itself returns (you drop it) the end iterator as you were deleting the last element
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.