金海佳学C++primer 练习9.27

来源:互联网 发布:sql 表中复制数据 编辑:程序博客网 时间:2024/05/18 01:17

查找并删除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.

原创粉丝点击