zoukankan      html  css  js  c++  java
  • c++删除容器中的奇数

    出自

    c++ primer(4th)282页,26题

    题意

    数组ia[]={0,1,1,2,3,5,8,13,21,55,89};把ia复制到一个list容器中。使用单个迭代器参数版本的erase()函数将list容器中的奇数元素值删掉。

    代码

    #include <iostream>
    #include <list>
    using namespace std;
    int main()
    {
        int ia[] = {0,1,1,2,3,5,8,13,21,55,89};
        list<int> ilist(ia, ia+11);
        list<int>::iterator beg = ilist.begin();
        for(;beg!=ilist.end();)
        {
            cout << *beg << " " << *beg % 2 << endl;
            if(1 == (*beg % 2))
                beg = ilist.erase(beg);
            else
                ++beg;
        }
        for(beg=ilist.begin(); beg!=ilist.end(); beg++)
            cout << "UU:" << *beg << endl;
        cout << ilist.size() << endl;
    }

    说明几点
    1. 指针就是迭代器

    2.容器初始化的一种方式就是:C c(beg, end)——C为容器类型名如,list;c为自己建立的容器名,如ilist;beg,end为容器的初始迭代器(复制不包括end所指元素)

    3. 删除操作 ilist.erase(beg)——删除beg迭代器指向的元素,返回一迭代器,指向被删除元素后面的元素

  • 相关阅读:
    oracle执行.sql文件
    rematch的基本用法
    dva的基本用法
    redux-saga基本用法
    react-redux的基本用法
    redux的基本概念
    mobx基本概念
    centos 编译安装Apache 2.4
    javascript动态添加一组input
    php配置文件语法
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/3175788.html
Copyright © 2011-2022 走看看