`
xitong
  • 浏览: 6190053 次
文章分类
社区版块
存档分类
最新评论

stl vector/list如何一边遍历一边删除

 
阅读更多


有时候我们在遍历stl的容器的时候需要删除一些不符合条件的item,这时候我们就要担心iterator是不是因为原始的数据的改变而发生改变,因此往往比较容易出现一些问题,

下面比较一下list 和 vector的两种一边遍历一边删除:
// list
list<int> lll;
// vector
// vector<int> lll;

lll.push_back(1);
lll.push_back(2);
auto tb = lll.begin(), te= lll.end();

for(;tb!=te;){
printf("%p", &*tb);
printf("%p", &*(lll.end()));
tb=lll.erase(tb);
}

list的能正常通过运行但是vector的却有断错误,问题出在哪里呢? 问题就是在于两个的erase函数的不同

List:
This effectively reduces the list size by the number of elements removed, calling each element's destructor before.

lists are sequence containers specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence. Compared to the other base sequence containers (vector and deque), lists are the most efficient container erasing at some position other than the beginning or the end of the sequence, and, unlike in these, all of the previously obtained iterators and references remain valid after the erasing operation and refer to the same elements they were referring before (except, naturally, for those referring to erased elements).

vector:
Because vectors keep an array format, erasing on positions other than the vector end also moves all the elements after the segment erased to their new positions, which may not be a method as efficient as erasing in other kinds of sequence containers (deque, list).

This invalidates all iterator and references to position (or first) and its subsequent elements.


不难发现list erase不会改不原来的iterator, vector 就会改变,因此我们只要在代码里稍微做一点点修改就可以改正这个问题:
for(;tb!=te;) ====》 for(;tb!=lll.end();), 另外我们也发现 vector 的 erase 需要整个vector 移动,这个代价十分高,所以尽量少用。
分享到:
评论

相关推荐

    STL中逆向删除

    STL中逆向遍历及删除 例子: map set vector list 等等

    【STL源代码】C++标准库STL源代码下载

    【STL源代码】中包含了许多常用的数据结构(如vector、list、map等)和算法(如排序、查找、遍历等)。通过阅读代码可以仔细研究这些数据结构和算法的实现,了解它们的内部工作原理和使用方式。

    标准模板库STL

    STL容器部分主要由头文件&lt;vector&gt;、&lt;list&gt;、、、、和组成。 (2)算法(Algorithms)。包括各种基本算法,如比较、交换、查找、排序、遍历操作、复制、修改、移除、反转、合并等等。 STL算法部分主要由头文件和...

    c++实现STL容器

    实现了vector list set map 可以使用iterator遍历 插入 删除等功能

    STL标准模板库的例子

    STL标准模板库(standard template library) 容器类(可以存储其他对象的对象),算法(一系列封装好的函数),迭代器(用于遍历操作的类) 容器可以直接存储对象,也可以存储对象的指针。成熟的程序员喜欢使用间接...

    stl容器set,map,vector之erase用法与返回值详细解析

    在使用 list、set 或 map遍历删除某些元素时可以这样使用,如下所示

    刷leetcode不用stl-leetcode:leetcode

    刷leetcode不用stl leetcode 刷题 27 就地删除数组元素 vector 双指针法 72 最小编辑距离 20 括号匹配 栈的应用 127 单词接龙 BFS广度优先搜索 146 LRUCache list unordered_map&lt;&gt; 88 有序数组 合并两个vector ...

    关于STL的erase()陷阱-迭代器失效问题的总结

    下面材料整理自Internet&...在使用 list、set 或 map遍历删除某些元素时可以这样使用: 1.1 正确写法1 std::list&lt; int&gt; List; std::list&lt; int&gt;::iterator itList; for( itList = List.begin(); itList != List.end();

    [原创]自己工作中常用的模板库,简化你的工作

    ☆ can initialize array/stl container with initalization list repeated. e.g. vector&lt;int&gt; a={1,2,3,45,2}; ◆ [macroLoop.hpp] ★ 当多条语句的差别仅仅是一个数字时,可以利用提供的循环宏简化成一条宏语句,...

    C++STL程序员开发指南【可搜索+可编辑】

    第3 章STL 技术原理· · · · · · · · · · · · · · · · · ·................................. 103 3-1 模板概述· · · · · · • · · · · · · · · · · · · ·.......................

    C++设计模式编程中的迭代器模式应用解析

    迭代器模式应该是最为熟悉的模式了,最简单的证明就是我在实现组合模式、享元模式、观察者模式中就直接用到了 STL 提供的迭代器来遍历 Vector 或者 List数据结构。 迭代器模式也正是用来解决对一个聚合对象的遍历...

    程序员面试刷题的书哪个好-CPP_Learning:记录下C++语言学习

    STL,vector,list 标准模版库,vector是动态数组/向量,list是双向链表。都是顺序容器。 C++多态,虚函数机制 基础:继承关系,有虚函数,父类指针指向子类。虚函数表 进程通信 进程线程差异 进程是最小的资源分配...

    C++进阶课程讲义_v1.0.4.pdf

    10.3.3常用的遍历算法 148 10.3.4常用的查找算法 152 10.3.5常用的排序算法 154 10.3.6常用的拷贝和替换算法 156 10.3.7常用的算术和生成算法 157 10.3.8常用的集合算法 158 10.4 STL综合案例 159 10.4.1案例学校...

    传智播客扫地僧视频讲义源码

    09_链表的删除和销毁 10_链表的逆置_传智扫地僧 11_链表的逆置_课堂答疑 12_课堂答疑pheadnextnext 13_中午课程回顾 14_传统链表和非传统链表 15_链表的技术体系推演 16_通用链表库集成和测试 17_C提高课程_day05-...

    数据结构、算法与应用:C++语言描述(原书第2版)第二部分

    5.4 vector的描述 5.5 在一个数组中实现的多重表 5.6 性能测量 5.7 参考及推荐读物 第6章 线性表——链式描述 6.1 单向链表 6.1.1 描述 6.1.2 结构chainNode 6.1.3 类chain 6.1.4 抽象数据类型linearList的扩充 ...

Global site tag (gtag.js) - Google Analytics