遍历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 ...
随机推荐
- 一起学习iOS开发专用词汇,每天记3个,助你变大牛-b
大家做开发最大的问题是什么?英语的问题应该困扰很多的同学的地方,我们提倡科学学习开发中的常用词汇.我们不要求大家有特别好的听.说.写,只要能够记住,能够认识这些常用词汇你以后的开发也将游刃有余.我们的 ...
- 3D touch 的 应用 --备用
在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch技术.3D Touch的触控技术,被苹果称为新一代多点触控技术.其实,就是此前在Apple Watch上采用的For ...
- Logger之Logger.getLogger(CLass)
之前一直在使用System.out.println()来调试.但是用这种方式开发项目部署到生产环境,会因为众多的控制台输出降低应用的性能.这时候Log4J就成为可平衡开发和部署应用的利器了. 在项目中 ...
- android 休眠唤醒机制分析(三) — suspend
本文转自:http://blog.csdn.net/g_salamander/article/details/7988340 前面我们分析了休眠的第一个阶段即浅度休眠,现在我们继续看休眠的第二个阶段 ...
- 转:Java开发牛人十大必备网站
原文来自于:http://www.importnew.com/7980.html 以下是我收集的Java开发牛人必备的网站.这些网站可以提供信息,以及一些很棒的讲座, 还能解答一般问题.面试问题等.质 ...
- ubuntu后台运行命令行
ubuntu 程序后台运行几个方法 . 程序后加上“&” ,即 “./myjob &”, 将命令放入到一个作业队列中,可以用命令“jobs” 查看 . 将1中的命令放在 “()”中, ...
- RUBY类特性
学习教材 class Person def initialize(name,age=18) @name=name @age=age @motherland="China" end ...
- 手工走一次OPENSTACK安装,掉一层皮啊
掉皮也是值得的,对OS的了解慢慢加深. 最近加入CS的Q群也学到不少.
- Wi-Fi定位,AP定位
Wi-Fi实时定位系统 基于Wi-Fi的无线局域网实时定位系统(Wi-Fi RTLS)结合无线局域网络(WLAN).射频识别(RFID)和实时定位等多种技术,广泛地应用在有无线局域网覆盖的区域,实现复 ...
- 【转】Android源代码查看途径
原文网址:http://www.it165.net/pro/html/201501/32967.html 作为一个android coder,多阅读android源码对提高android开发水平是很有 ...