一组数据,copy一份至list,一份至vector,list中erase奇数,vector中erase偶数

 #include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <algorithm>
int main()

{
    int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
    int lengthArray = sizeof(ia)/sizeof(int);
    cout<<"The length of ia is "<<lengthArray<<endl;

    vector<int> iVec;
    list<int> iList;
    for (int i = 0; i < lengthArray; ++i) {
       iVec.push_back(ia[i]);
       iList.push_back(ia[i]);
    }

    vector<int>::iterator iIter = iVec.begin();
    for (; iIter != iVec.end(); ) {
       if(*iIter%2 == 0){
           iIter = iVec.erase(iIter);
       } else {
           ++iIter;
           continue;
       }
    }

    list<int>::iterator lIter = iList.begin();
    for (; lIter != iList.end(); ) {
       if (*lIter%2) {
           lIter = iList.erase(lIter);
       } else {
           ++lIter;
           continue;
       }
    }

    cout<<"The vector after deleting even is :";
    for ( vector<int>::iterator iIter = iVec.begin(); iIter != iVec.end(); ++iIter) {
        cout<<*iIter<<ends;
    }
    cout<<endl;

 
    cout<<"The list after deleting odd is :";
    for ( list<int>::iterator iIter = iList.begin(); iIter != iList.end(); ++iIter) {
        cout<<*iIter<<ends;
    }
    cout<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值