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
*/
...全文
163 9 打赏 收藏 转发到动态 举报
写回复
用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都仍然有效,仍然指向原来的位置。
学习并掌握C++2.0(11+14+17+20)的新特性,学习线程及线程池的应用 ---------------------------------------------------给小白学员的3年学习路径及计划技术方面分三块:1.纯开发技术方向2.音视频流媒体专业方向3.项目实战---------------------------------------------------1.纯开发技术方向(1) C++必须要过硬(至少学会10本经典好书)(2) 系统级编程(Windows、Linux),必须特别熟练系统API,灵活运用(3) 框架与工具(Qt、MFC):必须精通其中一种。(4) 架构与设计模式:需要提升一个高度,不再是简单的编码,而是思维模式。(5) 驱动级别(如果有兴趣,可以深入到驱动级:包括Windows、Linux)(6) 最好学习点Java+Html+javascript等WEB技术。2.音视频流媒体专业方向(1) 音视频流媒体基础理论:   必须认真学会,否则看代码就是看天书(2) 编解码方向:精通h.264,h.265(hevc), 包括理论和各个开源库(ffmpeg,libx264,libx265,...)。(3) 直播方向:  精通各种直播协议(rtsp,rtmp,hls,http-flv,...), 钻研各个开源库(live555,darwin,srs,zlmediakit,crtmpserver,...)(4) 视频监控:  理论+开源库(onvif+281818)(EasyMonitor、iSpy、ZoneMinder(web)、...) 3.项目实战(1) Qt项目:  至少要亲手练习10个实战项目(网络服务器、多线程、数据库、图像处理、多人聊天、等等)(2)音视频项目:包括编解码、视频监控、直播等各个方向,都需要亲手实战项目,包括视频服务器、后台管理系统、前端播放器(多端)---------------------------------------------------  第1章 C++11新特性 41). nullptr关键字与新语法 42). auto和decltype类型推导 6 auto讲解 6 auto示例 7 decltype 83). for区间迭代 94). 初始化列表 105). 模板增强 11外部模板 11类型别名模板 12默认模板参数 126). 构造函数 13委托构造 13继承构造 147). Lambda 表达式 158). 新增容器 20std::array 20std::forward_list 21无序容器 22元组 std::tuple 239). 正则表达式 2610). 语言级线程支持 28多线程库简介 2811). 右值引用和move语义 31右值引用和move语义 32转移左值 3412). constexpr 35第2章 C++14新特性 36Lambda 函数 36类型推导 37返回值类型推导(Return type deduction) 37泛型lambda 39[[弃用的]]  [[deprecated]]属性 40二进制数字和数字分隔符 41第3章 C++17新特性 42安装GCC10.2 42安装msys2-x86_64-20200720 42更新镜像 42更新软件库 43安装 MinGW64 等必要的软件 43环境变量Path 43编译命令 43constexpr 44typename 45折叠表达式 47结构化绑定 48条件分支语句初始化 49聚合初始化 50嵌套命名空间 52lambda表达式捕获*this的值 53改写/继承构造函数 54用auto作为非类型模板参数 55__has_include 56fallthrough 57nodiscard 57maybe_unused 58第4章 C++20新特性 59编译命令 59concept 59typename 60explicit 61constinit 62位域变量的默认成员初始化 62指定初始化 63基于范围的for循环初始化 64放宽基于范围的for循环,新增自定义范围方法 65嵌套内联命名空间 66允许用圆括弧的值进行聚合初始化 67unicode字符串字面量 68允许转换成未知边界的数组 68likely和unlikely 69第5章 C++2.0(11/14/17/20)总结与分析 705.1 C语言与C++ 715.2 语言可用性的强化 725.2.1 常量 725.2.2 变量及其初始化 735.2.3 类型推导 745.2.4 控制流 765.2.5 模板 775.2.6 面向对象 815.3 语言运行期的强化 835.3.1 Lambda 表达式 835.3.2 右值引用 865.4 容器 885.4.1 线性容器 885.4.2 无序容器 895.4.3 元组 895.5 智能指针与内存管理 905.5.1 RAII 与引用计数 905.5.2 std::shared_ptr 905.5.3 std::unique_ptr 915.5.4 std::weak_ptr 91第6章 C++2.0多线程原理与实战 93什么是并发 93并发的方式 93为什么使用并发 95线程简介 96创建线程的三种方式 971. 通过函数 972.通过类对象创建线程 993.通过lambda表达式创建线程 101thread线程的使用 101互斥量与临界区 105期物Future 111条件变量 112原子操作 114内存模型 118第7章 C++2.0线程池原理与实战 120线程与线程池的基本原理 1201)、线程 1202)、线程的生命周期 1213)、什么是单线程和多线程 1214)、线程池 1225)、四种常见的线程池 123线程池的架构与流程 123线程池代码实战 125    

64,654

社区成员

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

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