指向vector或string中删除点之后位置的迭代器、引用和指针都会失效,这句话是否正确?

菜鸡的逆袭之路 2017-01-07 12:37:29
C++ Primer 5E p311中有一句话提到“指向vector或string中删除点之后位置的迭代器、引用和指针都会失效”,但根据下面的测试代码,似乎并不是这个道理
测试代码:

#include <iostream>
#include <vector>
#include <list>

using std::vector;
using std::list;
using std::cout;
using std::endl;
using std::end;

int main()
{
int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};

//! init
vector<int> vec(ia, end(ia));
list<int> lst(vec.begin(), vec.end());

//! remove odd value
for (auto it = lst.begin(); it != lst.end();)
if (*it & 0x1)
it = lst.erase(it);
else
++it;

//! remove even value
for (auto it = vec.begin(); it != vec.end();)
if (!(*it & 0x1))
it = vec.erase(it);
else
++it;

//! print
cout << "list : ";
for (auto i : lst) cout << i << " ";
cout << "\nvector : ";
for (auto i : vec) cout << i << " ";
cout << std::endl;

return 0;
}

为什么把vector中删掉元素之后的迭代器重新赋值给it之后程序仍然可以运行?
...全文
222 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
http://en.cppreference.com/w/cpp/container/vector/erase 1.Invalidates iterators and references at or after the point of the erase, including the end() iterator. 2.Return value Iterator following the last removed element. If the iterator pos refers to the last element, the end() iterator is returned. 请问这两句话矛盾吗?哪位老师能给详细讲一下,谢谢!
  • 打赏
  • 举报
回复
引用 1 楼 lunat 的回复:
http://www.cplusplus.com/reference/list/list/erase/ Return value An iterator pointing to the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence. Member type iterator is a bidirectional iterator type that points to elements. http://www.cplusplus.com/reference/vector/vector/erase/ Return value An iterator pointing to the new location of the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence. Member type iterator is a random access iterator type that points to elements.
老师,在循环条件中直接注明++it和在循环体中把erase函数的返回值赋值给it有什么区别?不都是指向下一个元素的迭代器吗?
lunat 2017-01-07
  • 打赏
  • 举报
回复
http://www.cplusplus.com/reference/list/list/erase/ Return value An iterator pointing to the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence. Member type iterator is a bidirectional iterator type that points to elements. http://www.cplusplus.com/reference/vector/vector/erase/ Return value An iterator pointing to the new location of the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence. Member type iterator is a random access iterator type that points to elements.
paschen 版主 2017-01-07
  • 打赏
  • 举报
回复
erase的返回值是删除元素的下一个元素的迭代器 你需要使用这种方式,而不能删除后再++it,因为删除元素之后迭代器已经失效 http://en.cppreference.com/w/cpp/container/vector/erase

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧