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. 绑定dropdownlist

    System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(); sqlconn.C; ...

  2. 创建局域网内远程git仓库,并将本地仓库push推到远程仓库中

    转载请注明出处 http://www.goteny.com/articles/2014/06/136.html http://www.cnblogs.com/zjjne/p/3778640.html ...

  3. codemirror 插件

    做在线词典编辑的时候.里面有些自定义标签.类似html标签一样. 为了让编辑编辑.改成了  <动词></动词> 所以引用了 codemirror插件 此插件绝对牛逼 它主要功能 ...

  4. 吃了单片机GPIO端口工作模式的大亏 ——关于强推挽输出和准双向口(弱上拉)的实际应用

    最近公司在进行一个项目,需要用到超声波测距的功能,于是在做好硬件电路,但在写控制程序时,却遇上了令我费解的事情. 当在单片机最小系统上调好输出频率40kHz,占空比50%的方波输出信号后,将程序烧至超 ...

  5. POJ3349 Snowflake Snow Snowflakes(哈希)

    题目链接. 分析: 哈希竟然能这么用.检查两片雪花是否相同不难,但如果是直接暴力,定会超时.所以要求哈希值相同时再检查. AC代码: #include <iostream> #includ ...

  6. HBase Endpoint

    引言   假设HBase某张表有1000个Region,里面存储着100万行数据,现在需要统计满足某些条件的行数,普通的做法是使用Filter(过滤条件),通过HBase API将满足过滤条件的行数据 ...

  7. Git Bash 使用心得

    1:下载Git  Git for Windows 2:点击安装,依次默认下一步 3:安装完成 4:设置SSH建立计算机与Github的链接 4.1 点击开始菜单找到Git Bash,并点击: 4.2 ...

  8. xshell中启动linux图形界面

    使用root用户执行xhost + IP为客户端机器IP,使用远程登录用户执行 DISPLAY=IP:0.0;export DISPLAY; 使用远程登录的用户执行: xhost +

  9. SVN和Maven及Jenkins(转)

    目前项目组在开发一个项目,由多个子模块构成,构建工具是maven,版本控制工具是svn.本文想对如何结合使用maven和svn提出一点初步的想法 一.只有svn的情况 首先考虑没有maven的情况.这 ...

  10. C primer plus 读书笔记第五章

    本章的标题是运算符,表达式和语句.主要研究如何处理数据. 示例代码展示了一个使用简单的while循环的代码,难度不大. 下面简单介绍本章的基本概念. 1.基本运算符. 基本运算符有:赋值运算符(C语言 ...