std::list 为什么这样设计??

MuteCoder 2016-03-03 10:45:42

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

int main()
{
std::list<int > nlist;

std::list<int >::iterator it = nlist.begin();
std::list<int >::iterator ite = nlist.end();
//nlist.push_back(10);
//printf("list.begin = %d\n", *it);//error if first get begin and then push_back
it++;
if(it==ite)
{
printf("list::end");
}

return 0;
}
//output
/*
/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../include/c++/4.1.2/debug/safe_iterator.h:224:
error: attempt to increment a past-the-end iterator.

Objects involved in the operation:
iterator "this" @ 0x0xffd37c84 {
type = N11__gnu_debug14_Safe_iteratorIN10__gnu_norm14_List_iteratorIiEEN15__gnu_debug_def4listIiSaIiEEEEE (mutable iterator);
state = past-the-end;
references sequence with type `N15__gnu_debug_def4listIiSaIiEEE' @ 0x0xffd37c84
}

Disallowed system call: SYS_kill
*/
...全文
170 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
MuteCoder 2016-03-03
  • 打赏
  • 举报
回复
引用 5 楼 l1095260368 的回复:

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

int main()
{
std::list<int > nlist;
;

std::list<int >::iterator it = nlist.begin();
nlist.push_back(1);
printf("nlist %d\n", *it);

return 0;
}
//output
/*
/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../include/c++/4.1.2/debug/safe_iterator.h:181:
    error: attempt to dereference a past-the-end iterator.

Objects involved in the operation:
iterator "this" @ 0x0xffc73294 {
type = N11__gnu_debug14_Safe_iteratorIN10__gnu_norm14_List_iteratorIiEEN15__gnu_debug_def4listIiSaIiEEEEE (mutable iterator);
  state = past-the-end;
  references sequence with type `N15__gnu_debug_def4listIiSaIiEEE' @ 0x0xffc73294
}

Disallowed system call: SYS_kill
*/

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

int main()
{
std::list<int > nlist;
nlist.push_back(1);
nlist.push_back(2);
nlist.push_back(3);
nlist.push_back(4);

std::list<int >::iterator it = nlist.begin();
std::list<int >::iterator ite = nlist.end();
nlist.push_back(5);
while(it !=ite )
{
printf("nlist %d\n", *it++);
}

return 0;
}
//output
/*
nlist 1
nlist 2
nlist 3
nlist 4
nlist 5
*/
这两段代码的差别在于是否是在std::list为空时先获取了begin,但差别这么大??
引用 7 楼 starytx 的回复:
插入数据可能会使迭代器失效,所以不要存储和使用end操作返回的迭代器
想了想,在list没有元素的时候it等于begin;同时it也等于end;那就说明list 迭代器是等同于链表的next指针的。 所以第一段代码即使在it=begin()后push_back一个元素,it也是等于end。
starytx 2016-03-03
  • 打赏
  • 举报
回复
插入数据可能会使迭代器失效,所以不要存储和使用end操作返回的迭代器
MuteCoder 2016-03-03
  • 打赏
  • 举报
回复
感觉迭代器设计的有问题啊
MuteCoder 2016-03-03
  • 打赏
  • 举报
回复

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

int main()
{
std::list<int > nlist;
;

std::list<int >::iterator it = nlist.begin();
nlist.push_back(1);
printf("nlist %d\n", *it);

return 0;
}
//output
/*
/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../include/c++/4.1.2/debug/safe_iterator.h:181:
    error: attempt to dereference a past-the-end iterator.

Objects involved in the operation:
iterator "this" @ 0x0xffc73294 {
type = N11__gnu_debug14_Safe_iteratorIN10__gnu_norm14_List_iteratorIiEEN15__gnu_debug_def4listIiSaIiEEEEE (mutable iterator);
  state = past-the-end;
  references sequence with type `N15__gnu_debug_def4listIiSaIiEEE' @ 0x0xffc73294
}

Disallowed system call: SYS_kill
*/

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

int main()
{
std::list<int > nlist;
nlist.push_back(1);
nlist.push_back(2);
nlist.push_back(3);
nlist.push_back(4);

std::list<int >::iterator it = nlist.begin();
std::list<int >::iterator ite = nlist.end();
nlist.push_back(5);
while(it !=ite )
{
printf("nlist %d\n", *it++);
}

return 0;
}
//output
/*
nlist 1
nlist 2
nlist 3
nlist 4
nlist 5
*/
这两段代码的差别在于是否是在std::list为空时先获取了begin,但差别这么大??
paschen 版主 2016-03-03
  • 打赏
  • 举报
回复
引用 2 楼 l1095260368 的回复:
[quote=引用 1 楼 paschen 的回复:] 因为你这个情况it一开始就已经等于nlist.end()了 (你list中没有任何东西) 该迭代器不可递增了!
难道没有数据时,begin == end??[/quote] 是的
MuteCoder 2016-03-03
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
因为你这个情况it一开始就已经等于nlist.end()了 (你list中没有任何东西) 该迭代器不可递增了!
难道没有数据时,begin == end??
MuteCoder 2016-03-03
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
因为你这个情况it一开始就已经等于nlist.end()了 (你list中没有任何东西) 该迭代器不可递增了!
第12行也是同样的输出错误。这又是为什么?先it = begin, 再push_back, 这是it理应等于push_back的那个元素吧
paschen 版主 2016-03-03
  • 打赏
  • 举报
回复
因为你这个情况it一开始就已经等于nlist.end()了 (你list中没有任何东西)

该迭代器不可递增了!
duke1996 2016-03-03
  • 打赏
  • 举报
回复
list.push(back),可能会是原有的地迭代器失效。 1.第一个例子中,当list为空的时候,it = nlist.begin();这个it是指向空节点的(比如一种实现,指向双链表当中的将来也不存储值的头结点。)。然后你push_back一个元素到nlist中,这个it还是指向空节点。本质上就是it此时已经失效了!如果需要的话,需要在push_back后,再次执行it=nlist.begin(); 2.第二个例子,在插入四个元素后,it指向第一个元素,ite指向最后一个元素的后一个元素(比如一种实现,指向双链表当中的将来也不存储值的头结点)。 然后push_back一个元素,实际上,这个动作,it或ite都仍然有效,仍然指向原来的位置。

65,186

社区成员

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

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