遍历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 ...
随机推荐
- 转:搭建Hive的图形界面
原文来自于:http://blog.csdn.net/w13770269691/article/details/17353595 今天想使用一下Hive的图形化工具HWI,我的Hive是0.12.0版 ...
- fragment的实现与互相通信
Android3.0后出来的新控件,主要是为了在平板和手机屏幕的兼容 实现效果: 点击Sd卡,出现SD目录下的所有文件和文件夹,点击外置Sd卡,出现外置Sd卡目录下的文件和文件夹.点击U盘,出现U盘目 ...
- 无向图求割点 UVA 315 Network
输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #in ...
- 深入JS系列学习4
深入JS系列学习4 Javascript 装载和执行 明白了JS的装载和执行,没有给出很好的解决方案,在IE下可用defer属性: 浏览器对于Javascript的运行有两大特性:1)载入后马上执行, ...
- WIndows系统下mysql-noinstall安装配置
环境: Windowsmysql-noinstall-5.0.37-win32.zip 一.下载MySQL http://www.mysql.com/downloads 二.安装过程 1.解压缩mys ...
- ASP.NET NuGet to install the mvc 5.2.2
http://www.nuget.org/packages/Microsoft.AspNet.Mvc
- Java并发编程:Lock
Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.l ...
- [置顶] Hash查找,散列查找
//Hash.h #ifndef HASH_H #define HASH_H #define HASH_ARR_SIZE 100 #define FILL -1 #include <stdlib ...
- Liferay 6.1开发学习
http://www.huqiwen.com/2013/01/10/liferay-6-1-development-study-17-springmvc-portlet/ http://www.blo ...
- 外键约束列并没有导致大量建筑指数library cache pin/library cache lock
外键约束列并没有导致大量建筑指数library cache pin/library cache lock 清除一个100大数据表超过一百万线,发现已经运行了几个小时: delete B001.T_B1 ...