#include<vector> #include<list> #include<iostream> using namespace std; int main() { vector<int> vec={0,1,1,2,3,5,8,21,55,89}; list<int> li={0,1,1,2,3,5,8,21,55,89}; auto ve=vec.begin(); auto it=li.begin(); while(ve!=vec.end()) { if(*ve%2) ++ve; else ve=vec.erase(ve); } while(it!=li.end()) { if(*it%2) it=li.erase(it); else ++it; } for(auto v:vec) cout<<v<<" "; cout<<endl; for(auto i:li) cout<<i<<" "; cout<<endl; return 0; }
运行结果:
删除vector中的偶数元素,删除list中的奇数元素,布布扣,bubuko.com
时间: 2024-10-14 10:32:57