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. 关于 从别人电脑上 高版本的 Xcode上拷贝过来的项目的 不能运行模拟器的 解决方法

    如图 从别人电脑上 拷贝过来的  工程  打开后  点击 iOS  Device  只有  一个选项  没有模拟器.这说明 自己的 Xcode 的版本比 创建这个工程所用的版本低.所以 要睇啊你tar ...

  2. SAE、搜狐云景和百度云之初见

    近期有需求将我们的应用部署到公有云的服务平台上,于是找了几家公有云服务做了一下调研, 首先对比一下他们提供的功能: 功能 SAE 搜狐云景 百度云 版本控制工具 svn  GIT,和百度云的比起来,用 ...

  3. 转:Hprose for php(二)——服务器

    文章来自于:http://blog.csdn.net/half1/article/details/21252879 本文将介绍Hprose for php服务器的更多细节 1.发布服务 Hprose提 ...

  4. <frameset><frame><iframe>网页框架

    这几个标签都属于同一类功能,就是框架内镶功能: 1)<frameset>意为把页面分解成一定部分,让每一部分显示不同的内镶框架,如(请复制到DW尝试): <html> < ...

  5. VitamioBundle-master

    1. 下载资源 (1) 核心插件 VitamioBundle 下载地址:https://github.com/yixia/VitamioBundle (2) 官方示例 VitamioDemo 下载地址 ...

  6. (2015年郑州轻工业学院ACM校赛题) G 矩阵

    看这道题的时候就感觉用一点动归思想+暴力 就能过了. #include<stdio.h> #include<iostream> #include<stack> #i ...

  7. jsp+servlet 上传图片实例

    最近,在弄android小程序,现在需要做一个服务器后端,这里我下载了一个商城源码,想添加商品图片,这里在网上找了一个实例 引用: http://blog.csdn.net/shuwei003/art ...

  8. 实Schur分解

        前面已经说过LU,Cholesky和QR分解,这次介绍的是实Schur分解.对这个分解的定义是任意一个矩阵A,可有如下形式的分解:               U*A*U' = B;其中B是拟 ...

  9. JAVA中的Transient

    Java中的transient,是用于声明序列化的时候不被存储的. 例子: package ThreadTest; /** * Created with IntelliJ IDEA. * User: ...

  10. RHEL6配置IP

    修改配置文件:/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0BOOTPROTO=noneBROADCAST=192.168.10.255HW ...