擦除时std :: list的end()迭代器位置是否会发生变化?

Sub*_*way 4 c++ containers iterator erase

在下面的循环中,我使用的是预先计算的结束迭代器:

std::list::iterator end = MyList.end();

for (std::list::iterator it = MyList.begin(); it != end ;) 
    it = MyList.erase(it);
Run Code Online (Sandbox Code Playgroud)

当擦除std :: list中的元素时,MyList.end()是否可以更改其值以便更新end != MyList.end()

For*_*veR 5

没有.

n3376 23.3.5.4

iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
Run Code Online (Sandbox Code Playgroud)

效果:仅使迭代器和对已擦除元素的引用无效.

  • 为了完整起见,请记住这适用于`std :: list`和其他一些容器类型,但对于所有容器类型(例如`std :: vector`)都不适用. (8认同)