在使用 std::find
找到一个元素后,我需要从 std::list
中删除它。使用列表的 end()
调用 std::list::erase
的行为是什么?我的情况是这样的:
std::list<T> mylist;
T value;
std::list::iterator it = std::find(mylist.begin(), mylist.end(), value);
std::list::iterator next = mylist.erase(it);
If position (or the range) is valid, the function never throws exceptions (no-throw guarantee). Otherwise, it causes undefined behavior.
但我不知道 end()
是否被认为是有效的。
最佳答案
该站点使用了含糊(并且可以说是不正确的)术语“有效”,但库规范 (C++11 23.2.3) 使用了更具体的术语“可解引用”——这意味着迭代器必须引用一个对象。尾后迭代器不可取消引用,因此删除它会产生未定义的行为。
关于c++ - 删除 `end()` 的 `std::list` 的行为是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15116676/