std::list::erase

Erase elements

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过程中删除元素后继续遍历过程的更多相关文章

  1. 遍历List过程中删除元素的正确做法(转)

    遍历List过程中删除元素的正确做法   public class ListRemoveTest {     3 public static void main(String[] args) { 4 ...

  2. map在遍历数据的过程中删除数据不出错

    // Iterator<Map.Entry<String,Long>> entries = Map.entrySet().iterator();                 ...

  3. /编写一个函数,要求从给定的向量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:$ */ /* 编写一个 ...

  4. javascript在数组的循环中删除元素

    在开发JavaScript应用的过程中,经常会遇到在循环中移除指定元素的需求. 按照常规的思路,就是对数组进行一个for循环,然后在循环里面进行if判断,在判断中删除掉指定元素即可. 但是实际情况往往 ...

  5. 遍历并remove HashMap中的元素时,遇到ConcurrentModificationException

    遍历并remove HashMap中的元素时,遇到ConcurrentModificationException for (Map.Entry<ImageView, UserConcise> ...

  6. 如何从List中删除元素

    从List中删除元素,不能通过索引的方式遍历后删除,只能使用迭代器. 错误的实现 错误的实现方法 public class Demo {     public static void main(Str ...

  7. Java中list在循环中删除元素的坑

    JAVA中循环遍历list有三种方式for循环.增强for循环(也就是常说的foreach循环).iterator遍历. 1.for循环遍历list for(int i=0;i<list.siz ...

  8. Jquery中删除元素方法

    empty用来删除指定元素的子元素,remove用来删除元素,或者设定细化条件执行删除 语法: empty() remove(expr); empty用来删除指定元素的子元素,remove用来删除元素 ...

  9. [译]如何在迭代字典的过程中删除其中的某些item(Python)

    最好不要在迭代的过程中删除.你可以使用解析式和filter过滤. 比方说: {key:my_dict[key] for key in my_dict if key !="deleted&qu ...

随机推荐

  1. 替代 yunio , 推荐一个国外的云存储(同步盘) wuala(这个网站也挂了)——功能评测非常全面

    替代 yunio , 推荐一个国外的云存储(同步盘) wuala 话说自从云诺开始收费后,我的文件便再没同步或备份过,前天没事便找了一下,发现一个国外的网盘,注册送 5 G 空间(对我来说同步日常文件 ...

  2. Floyd 算法 打印路径模板

    #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> ...

  3. 源代码管理工具TFS2013安装与使用

    最近公司新开发一个项目要用微软的TFS2013进行项目的源代码管理,以前只是用过SVN,从来没有用过TFS,所以在网上百度.谷歌了好一阵子来查看怎么安装和配置,还好花了一天时间总算是初步的搞定了,下面 ...

  4. HDOJ(HDU) 1985 Conversions(汇率转换)

    Problem Description Conversion between the metric and English measurement systems is relatively simp ...

  5. HDOJ的题目分类

    模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 10 ...

  6. Iterator荟萃

    package com.starain.Iterator;/*代码整理快捷键为Ctrl+Shift+F * main方法输入快捷键main字符+Alt+/ * 输出快捷键sysout字符+Alt+/* ...

  7. python学习笔记(集合的使用)

    集合 集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员 为什么需要集合? 集合的作用 1 .列表去重复数据 按照现有知 ...

  8. 我与Ubuntu的5年共成长

    初次接触Ubuntu那还是2010年的初夏,那年大学二年级即将结束,为了增加计算机学院学生的技术能力和就业竞争力,学校组织了很多培训机构.公司企业来学校做技术宣讲.分享等 记得有一个企业是做Mp3 M ...

  9. [转] 用GDB调试程序(五)

    转:http://blog.csdn.net/haoel/article/details/2883 查看运行时数据———————        在你调试程序时,当程序被停住时,你可以使用print命令 ...

  10. 判断两条直线的位置关系 POJ 1269 Intersecting Lines

    两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...