遍历std::list过程中删除元素后继续遍历过程
std::list::erase
Removes from the list container either a single element (position) or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, which are destroyed.
Unlike other standard sequence containers, list and forward_list objects are specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence.
返回值
An iterator pointing to the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence.
] = { , , , , , , , , , }; list<); vector<int> iVec; for(list<int>::iterator it = iList.begin(); it != iList.end(); it++) { iVec.push_back(*it); ) { it = iList.erase(it); it--; } } cout << "iVec: "; for(auto& i : iVec) { cout << i << " "; } cout << endl; cout << "iList: "; for(auto& i : iList) { cout << i << " "; } cout << endl;
Output:
iVec: 2 3 6 4 1 17 4 9 25 4
iList: 2 3 6 1 17 9 25
或者:
for(list<int>::iterator it = iList.begin(); it != iList.end();) { iVec.push_back(*it); ) { it = iList.erase(it); } else { it++; } }
遍历std::list过程中删除元素后继续遍历过程的更多相关文章
- 遍历List过程中删除元素的正确做法(转)
遍历List过程中删除元素的正确做法 public class ListRemoveTest { 3 public static void main(String[] args) { 4 ...
- map在遍历数据的过程中删除数据不出错
// Iterator<Map.Entry<String,Long>> entries = Map.entrySet().iterator(); ...
- /编写一个函数,要求从给定的向量A中删除元素值在x到y之间的所有元素(向量要求各个元素之间不能有间断), 函数原型为int del(int A ,int n , int x , int y),其中n为输入向量的维数,返回值为删除元素后的维数
/** * @author:(LiberHome) * @date:Created in 2019/2/28 19:39 * @description: * @version:$ */ /* 编写一个 ...
- javascript在数组的循环中删除元素
在开发JavaScript应用的过程中,经常会遇到在循环中移除指定元素的需求. 按照常规的思路,就是对数组进行一个for循环,然后在循环里面进行if判断,在判断中删除掉指定元素即可. 但是实际情况往往 ...
- 遍历并remove HashMap中的元素时,遇到ConcurrentModificationException
遍历并remove HashMap中的元素时,遇到ConcurrentModificationException for (Map.Entry<ImageView, UserConcise> ...
- 如何从List中删除元素
从List中删除元素,不能通过索引的方式遍历后删除,只能使用迭代器. 错误的实现 错误的实现方法 public class Demo { public static void main(Str ...
- Java中list在循环中删除元素的坑
JAVA中循环遍历list有三种方式for循环.增强for循环(也就是常说的foreach循环).iterator遍历. 1.for循环遍历list for(int i=0;i<list.siz ...
- Jquery中删除元素方法
empty用来删除指定元素的子元素,remove用来删除元素,或者设定细化条件执行删除 语法: empty() remove(expr); empty用来删除指定元素的子元素,remove用来删除元素 ...
- [译]如何在迭代字典的过程中删除其中的某些item(Python)
最好不要在迭代的过程中删除.你可以使用解析式和filter过滤. 比方说: {key:my_dict[key] for key in my_dict if key !="deleted&qu ...
随机推荐
- Python三元表达式
我们知道Python没有三元表达式,但是我们通过技巧达到三元表达式的效果. 摘自<Dive Into Python>: 在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的 ...
- [EXCEL] 在单元格中自动输入时间和日期
选中需输入的单元格,直接按下“Ctrl+:”组合键可输入当前日期:如果直接按下“Ctrl+Shift+:”组合键即可输入当前时间:当然也可以在单元格中先输入其他文字然后再按以上组合键,如先输入“当前时 ...
- refresh的停车场
题目描述 refresh最近发了一笔横财,开了一家停车场.由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多.当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先 进入停车 ...
- 【转】深层次探讨mutex与semaphore之间的区别(下)
原文网址:http://blog.chinaunix.net/uid-23769728-id-3173282.html 这篇博文很长,虽然这是下篇,但还没结束,benchmark方面的东西正在进行中, ...
- Hadoop MapReduce InputFormat/OutputFormat
InputFormat import java.io.IOException; import java.util.List; /** * InputFormat describes the input ...
- 【宽搜】【并查集】Vijos P1015 十字绣
题目链接: https://vijos.org/p/1015 题目大意: n*m的网格,线只能在网格的顶点处才能从布的一面穿到另一面.每一段线都覆盖一个单位网格的两条对角线之一,而在绣的过程中,一针中 ...
- Silverlight 模板(Template)使用
模板(Template)是控件另一种样式 它和样式(style)不同的是它允许已有的控件进行组合新的一个控件样式 那么先看一下最简单Template代码 xaml代码 <Button Conte ...
- yui datatable动态修改行号
相关函数 getRecord :YAHOO.widget.Record getRecord ( row ) For the given identifier, returns the associa ...
- 自定义AuthorizeAttribute
原文地址:http://www.cnblogs.com/shanyou/archive/2010/03/29/1699511.html 网站的权限判断是一个非常普遍的需求,从文章ASP.NET MVC ...
- JavaScript String 对象实例深入研究
本文主要介绍并分析JavaScript中String对象的具体用法,以及和String对象相关的方法,方便开发者在JavaScript开发中更好地处理字符串. 1. 介绍 String 对象,对字符串 ...