在实现LRU算法的时候lru_list 開始用的是deque 可是由于害怕其在插入删除上的迭代器失效情况的诡异情况。遂用list取代之。

在数据量比較大的时候性能不是非常好。性能优化分析的时候决定用deque替换回来。于是对deque迭代器失效的情况好好研究了一下:

c++ primer如此写道:

1.在deque容器首部或者尾部插入元素不会使得不论什么迭代器失效。 
2.在其首部或尾部删除元素则仅仅会使指向被删除元素的迭代器失效。

 
3.在deque容器的不论什么其它位置的插入和删除操作将使指向该容器元素的全部迭代器失效。

stackoverflow上对此的讨论:Confusion
on iterators invalidation in deque

I'm bit confused regarding iterator invalidation in deque. (In the context of this question)

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.

Following is the excerpts from SGI site:

The semantics of iterator invalidation for deque is as follows. Insert (including push_front and push_back)
invalidates all iterators that refer to a deque. Erase in the middle of a deque invalidates all iterators that refer to the deque. Erase at the beginning or end of a deque (including pop_front and pop_back)
invalidates an iterator only if it points to the erased element.

IMHO, deque is collection of blocks with first block growing in one direction and the last block in opposite direction.

  -   -  -
- - -
| - - ^
| - - |
V - - |
- - -
- - -

push_back,
push_front
 should not have any impact on deque iterators ( I agree with Josuttis).

What is the correct explanation?

what the standard say on this?

完美的答案:

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迭代器失效的困惑?的更多相关文章

  1. C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用

    序列性容器::(vector和list和deque)   erase迭代器不仅使所有指向被删元素的迭代器失效,而且使被   删元素之后的所有迭代器失效,所以不能使用erase(iter++)的方 式, ...

  2. C++ STL 迭代器失效问题

    之前看<C++ Primier>的时候,也解到在顺序型窗口里insert/erase会涉及到迭代器失效的问题,并没有深究.今天写程序的时候遇到了这个问题. 1 莫名其妙的Erase 最初我 ...

  3. STL的erase()陷阱-迭代器失效总结

    下面材料整理自Internet&著作. STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.s ...

  4. C++ Primer : 第九章 : 顺序容器的操作以及迭代器失效问题

    顺序容器的添加.访问.删除操作以及forward_list的特殊操作,还有迭代器失效问题. 一.向容器添加元素 // array不支持这些操作 // forward_list有自己撰于的版本的inse ...

  5. C++ STL中迭代器失效的问题

    my_container.erase(iter); 其中my_container是STL的某种容器,iter是指向这个容器中某个元素的迭代器.如果不是在for,while循环中,这种方式删除元素没有问 ...

  6. 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化

    1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...

  7. STL源代码分析--迭代摘要、迭代器失效汇总

    Vector 1.内部数据结构:连续存储,比如数组. 2.随机訪问每一个元素,所须要的时间为常量. 3.在末尾添加或删除元素所需时间与元素数目无关,在中间或开头添加或删除元素所需时间随元素数目呈线性变 ...

  8. stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list

    stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...

  9. C++迭代器失效的几种情况总结

    一.序列式容器(数组式容器) 对于序列式容器(如vector,deque),序列式容器就是数组式容器,删除当前的iterator会使后面所有元素的iterator都失效.这是因为vetor,deque ...

随机推荐

  1. 小学生都能学会的python(文件操作)

    小学生都能学会的python(文件操作) 1. open("文件路径", mode="模式", encoding="编码") 文件的路径: ...

  2. webpack的热更新

    webpack的热更新是如何做到的?说明其原理? webpack的热更新又称热替换(Hot Module Replacement),缩写为HMR. 这个机制可以做到不用刷新浏览器而将新变更的模块替换掉 ...

  3. webpack基础知识点

    webpack 是一个现代的 JavaScript 应用程序的模块打包器(module bundler). 入口(Entry) webpack 将创建所有应用程序的依赖关系图表(dependency ...

  4. 2015 Multi-University Training Contest 8 hdu 5384 Danganronpa

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  5. CSS学习(三)

    CSS 分组 和 嵌套 选择器 分组选择器 h1,h2,p { color:green; } 嵌套选择器 <!DOCTYPE html> <html> <head> ...

  6. Java String内存释放

    Java String内存释放 这是一个坑,Java对于String对象,不进行内存的回收: 处理大数据量的时候,少用String. 与JDK有关系:jdk1.6环境下,内存只占用10M,jdk1.8 ...

  7. Double 与 Float 的值的比較结果

    首先看geeksforgeeks上的两个程序: 程序1: #include<stdio.h> int main() { float x = 0.1; if (x == 0.1) print ...

  8. java-集合类(二)

    使用场景 1.多线程 (1)在jdk1.5之前原始的集合类中,仅仅有vector.stack.hashtable.enumeration等是线程安全的,其它的都是非线程安全的. 非线程安全的集合在多线 ...

  9. 仿写从iOS8开始支持的UIAlertController:BGAAlertController-Android

    工作以来公司UI设计师出的Android效果图都是iOS风格的UIAlertView和UIActionSheet,新项目还是用原来那一套,不想重复造轮子,所以仿写了从iOS8开始支持的UIAlertC ...

  10. Linux就该这么学 20181003(第三章管道符)

    参考链接https://www.linuxprobe.com/ 输入输出重定向 标准输入重定向STDIN 文件描述符0 默认键盘输入 标准输出重定向STOUT 文件描述符1 默认输出到屏幕 错误输出重 ...