list删除数据时的迭代器问题

abcdef0966 2009-09-02 09:36:32
这段代码是想删去list中的数值6,为什么运行时会崩溃呢
#include <iostream>
#include <list>
using namespace std;

void print(list<int>);

int main()
{
list<int> array;
array.push_back(1);
array.push_back(6);
array.push_back(6);
array.push_back(3);
//删除array数组中所有6
list<int>::iterator itor;
list<int>::iterator itor2;

for (itor = array.begin(); itor != array.end();)
{
if (6 == *itor)
{
itor2 = itor;

array.erase(itor2);

}

itor++;
}
print(array);
return 0;
}
void print(list<int> v)
{
cout << "\n vector size is:" << v.size() << endl;
list< int >::iterator p = v.begin();
while ( p != v.end() )
cout << *p++ << endl;
}
...全文
607 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2009-09-03
  • 打赏
  • 举报
回复
不冲突的。
abcdef0966 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 taodm 的回复:]
楼主自己去认真看《effective stl》,别懒了。
[/Quote]

我现在刚入门,看的是c++ primer
sayhorse 2009-09-03
  • 打赏
  • 举报
回复
            itor2 = itor;

array.erase(itor2);


删除itor2时,itor也会失效,
itor在删除前需要移动到前一个或者后一个元素位置。
taodm 2009-09-03
  • 打赏
  • 举报
回复
楼主自己去认真看《effective stl》,别懒了。
crescent_star 2009-09-03
  • 打赏
  • 举报
回复
erase(iter++);
kakashi0309 2009-09-02
  • 打赏
  • 举报
回复
list::erase
iterator erase(iterator it);
iterator erase(iterator first, iterator last);
The first member function removes the element of the controlled sequence pointed to by it. The second member function removes the elements of the controlled sequence in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists.

Erasing N elements causes N destructor calls. No reallocation occurs, so iterators and references become invalid only for the erased elements.

mstlq 2009-09-02
  • 打赏
  • 举报
回复
这个迭代器可能失效,也可能有效……
撇开具体实现来谈这个是没有意义的……

除非拿出一个STL库的源码来分析,某则谈什么都多余……
迭代器是不是用指针来实现的,可能是可能不是,不确定
迭代器自增是通过查找iter->next元素得到的吗,可能是可能不是,不确定

不确定因素太多,索性不理……
abcdef0966 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mstlq 的回复:]
C/C++ code
#include<iostream>
#include<list>usingnamespace std;void print(list<int>);int main()
{
list<int> array;
array.push_back(1);
array.push_back(6);
array.push_back(6);
array¡­
[/Quote]

这种改法我也知道的,但是我对迭代器失效理解不深,
上面的代码如果是用vector的话,至少运行时不会崩溃。

刚看到一个帖子里面讲到
vector相当于一个数组,当你在中间删除内容的时候,后面的数据会前移

list相当于一个双向链表,删除数据只是影响当前的节点,对前后数据没有影响

所以执行erase之后,
对vector,后面的数据向前移动,itor指向了被删数据后面的一个元素
对list,itor被删相当于删除了链表中的结点,故而itor++也就不能找到下一个节点了,所以运行时程序崩溃了

是这样理解的吗?
mstlq 2009-09-02
  • 打赏
  • 举报
回复

#include <iostream>
#include <list>
using namespace std;

void print(list<int>);

int main()
{
list<int> array;
array.push_back(1);
array.push_back(6);
array.push_back(6);
array.push_back(3);
//删除array数组中所有6
list<int>::iterator itor;
list<int>::iterator itor2;

for (itor = array.begin(); itor != array.end();)
{
if (6 == *itor)
itor=array.erase(itor);
else
itor++;
}
print(array);
system("pause");
return 0;
}
void print(list<int> v)
{
cout << "\n vector size is:" << v.size() << endl;
list< int >::iterator p = v.begin();
while ( p != v.end() )
cout << *p++ << endl;
}
mstlq 2009-09-02
  • 打赏
  • 举报
回复

iterator erase( iterator loc );
The erase method either deletes the element at location loc, or deletes the elements between start and end (including start but not including end). The return value is the element after the last element erased.

请楼主好好利用此函数的返回值
yang_e_2009 2009-09-02
  • 打赏
  • 举报
回复
任何添加或者删除容器元素的行为都有可能导致迭代器失效
--<<C++Primer>>
mstlq 2009-09-02
  • 打赏
  • 举报
回复
array.erase(itor2);
erase导致迭代器失效……

64,676

社区成员

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

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