查找并删除forward_list中的奇数元素
Practice 9.27
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>
#include <cmath>
#include <deque>
#include <cstring>
#include <forward_list>
using namespace std;
void find_and_delete(forward_list<int> & forlst) {
auto pre = forlst.before_begin();
auto cur = forlst.begin();
while(cur != forlst.end()) {
if((*cur) & 1) {
cur = forlst.erase_after(pre);
}
else {
pre = cur;
cur++;
}
}
}
void print(forward_list<int> forlst) {
for(auto i : forlst) {
cout << i << " ";
}
cout << endl;
}
int main() {
forward_list<int> forlst = {0, 1, 2, 3, 4, 5, 6, 7, 8};
find_and_delete(forlst);
cout << "The remain forward_list is: " << endl;
print(forlst);
return 0;
}
Output
The remain forward_list is:
0 2 4 6 8
When all else is lost the future still remains.