C++ Primer(第5版) 练习 9.27
练习 9.27 编写程序,查找并删除forward_list中的奇数元素。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp
/*************************************************************************
> File Name: ex9.27.cpp
> Author:
> Mail:
> Created Time: Tue 27 Feb 2024 08:37:21 AM CST
************************************************************************/
#include<iostream>
#include<forward_list>
using namespace std;
int main(){
forward_list<int> flst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto prev = flst.before_begin();
auto curr = flst.begin();
while(curr != flst.end()){
if(*curr % 2 != 0){
curr = flst.erase_after(prev);
}
else{
prev = curr;
++curr;
}
}
for(auto f : flst){
cout<<f<<" ";
}
cout<<endl;
return 0;
}