deque迭代器失效的困惑?
在实现LRU算法的时候lru_list 開始用的是deque 可是由于害怕其在插入删除上的迭代器失效情况的诡异情况。遂用list取代之。
在数据量比較大的时候性能不是非常好。性能优化分析的时候决定用deque替换回来。于是对deque迭代器失效的情况好好研究了一下:
c++ primer如此写道:
1.在deque容器首部或者尾部插入元素不会使得不论什么迭代器失效。
2.在其首部或尾部删除元素则仅仅会使指向被删除元素的迭代器失效。
3.在deque容器的不论什么其它位置的插入和删除操作将使指向该容器元素的全部迭代器失效。
stackoverflow上对此的讨论:Confusion
on iterators invalidation in deque
|
完美的答案:
IMHO, deque is collection of blocks with first block growing in one direction and the last block in opposite direction.
Your opinion is your prerogative, but it's wrong. :)
deque
is
such a container semantically, but in terms of implementation it's designed to be implemented by one or more blocks of memory. C++'s
iterator invalidation rules come from implementation, so this is why. Arguably this is a small abstraction leak but, well, whatever.
The SGI STL documentation is not the proper documentation to read, because the
SGI STL is not the C++ Standard Library. Unfortunately, Josuttis is one of those people who calls it "the STL", and this has led to your confusion.
Following is the excerpts from -- The C++ Standard Library: A Tutorial and Reference, By Nicolai M. Josuttis
Any insertion or deletion of elements other than at the beginning or end invalidates all pointers, references, and iterators that refer to elements
of the deque.
Put simply, this passage from Josuttis is misleading in implying that the insertion or deletion of elements thatare at
the beginning or end do not invalidate pointers, references or iterators … though it's worth noting that he never comes out and asserts this outright.
Here are the real, proper, official rules for std::deque
:
C++03
Insertion: all iterators and references are invalidated, unless the inserted member is at an end (front or back) of the deque (in which case all
iterators are invalidated, but references to elements are unaffected) [23.2.1.3/1]Erasure: all iterators and references are invalidated, unless the erased members are at an end (front or back) of the deque (in which case only iterators
and references to the erased members are invalidated) [23.2.1.3/4]Resizing: as per insert/erase [23.2.1.2/1]
C++11
Insertion: all iterators and references are invalidated, unless the inserted member is at an end (front or back) of the deque (in which case all
iterators are invalidated, but references to elements are unaffected) [23.3.3.4/1]Erasure: erasing the last element invalidates only iterators and references to the erased elements and the past-the-end iterator; erasing the first
element invalidates only iterators and references to the erased elements; erasing any other elements invalidates all iterators and references (including the past-the-end iterator) [23.3.3.4/4]Resizing: as per insert/erase [23.3.3.4/1]
Further reading
deque迭代器失效的困惑?的更多相关文章
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
序列性容器::(vector和list和deque) erase迭代器不仅使所有指向被删元素的迭代器失效,而且使被 删元素之后的所有迭代器失效,所以不能使用erase(iter++)的方 式, ...
- C++ STL 迭代器失效问题
之前看<C++ Primier>的时候,也解到在顺序型窗口里insert/erase会涉及到迭代器失效的问题,并没有深究.今天写程序的时候遇到了这个问题. 1 莫名其妙的Erase 最初我 ...
- STL的erase()陷阱-迭代器失效总结
下面材料整理自Internet&著作. STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.s ...
- C++ Primer : 第九章 : 顺序容器的操作以及迭代器失效问题
顺序容器的添加.访问.删除操作以及forward_list的特殊操作,还有迭代器失效问题. 一.向容器添加元素 // array不支持这些操作 // forward_list有自己撰于的版本的inse ...
- C++ STL中迭代器失效的问题
my_container.erase(iter); 其中my_container是STL的某种容器,iter是指向这个容器中某个元素的迭代器.如果不是在for,while循环中,这种方式删除元素没有问 ...
- 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化
1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...
- STL源代码分析--迭代摘要、迭代器失效汇总
Vector 1.内部数据结构:连续存储,比如数组. 2.随机訪问每一个元素,所须要的时间为常量. 3.在末尾添加或删除元素所需时间与元素数目无关,在中间或开头添加或删除元素所需时间随元素数目呈线性变 ...
- stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list
stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...
- C++迭代器失效的几种情况总结
一.序列式容器(数组式容器) 对于序列式容器(如vector,deque),序列式容器就是数组式容器,删除当前的iterator会使后面所有元素的iterator都失效.这是因为vetor,deque ...
随机推荐
- css所有属性(table,行列组)总结
概述: CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: CSS声明总是以分号(;)结束,声明组以大括号({})括起来: 一.注释: CSS注释以 "/*" 开始, ...
- tp框架 JS里面获取session
var var_name="{:session('xxxxx')}"; 用大括号 这个方法可以获取session
- HAVING使用子查询
HAVING使用子查询 //查询各部门平均工资,显示平均工资大于 //公司整体平均工资的记录 select deptno,avg(sal) from emp group by ...
- 2015 Multi-University Training Contest 6 hdu 5358 First One
First One Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- Apache Tez 0.7、0.83、 0.82 安装、调试笔记
———————————————————— 准备 Tez 编译环境 ———————————————————— 1 需要的支持 tez0.7 需要 Hadoop 2.60 以上 2 需要的 linux 相 ...
- 学一下HDFS,很不错(大数据技术原理及应用)
http://study.163.com/course/courseMain.htm?courseId=1002887002 里面的HDFS这一部分.
- 一分钟了解Android横竖屏 mdpi hdpi xhdpi xxhdpi xxxhdpi
DPI:每英寸像素数 简单的屏幕分辨率计算方法: DisplayMetrics metrics = this.getResources().getDisplayMetrics(); float den ...
- 蓝桥杯 - 带分数 (DFS)
历届试题 带分数 时间限制:1.0s 内存限制:256.0MB 问题描写叙述 100 能够表示为带分数的形式:100 = 3 + 69258 / 714. 还能够表示为:10 ...
- Hadoop解析--MapReduce
从本篇博客開始咱们一起来具体了解Hadoop的每一个部分.我们在上篇博客中介绍了HDFS,MapReduce,MapReduce为了更有效率事实上是建立在HDFS之上的.有了分布式的文件系统,我们就能 ...
- 一些.NET 项目中经常使用的类库
Web自己主动化測试 Watin Selenium Selenium git .net 集合类扩展实现C5 Subscriber/Publisher 模式 Rx Nats 防御式编程 断言库 流 ...