#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;
}
一组数据,copy一份至list,一份至vector,list中erase奇数,vector中erase偶数
最新推荐文章于 2023-06-13 16:51:20 发布