0
点赞
收藏
分享

微信扫一扫

数学建模【遗传算法】

舍予兄 02-29 10:30 阅读 2

C++ Primer(第5版) 练习 9.27

练习 9.27 编写程序,查找并删除forward_list中的奇数元素。

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
/*************************************************************************
	> 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;
}
运行结果显示如下

在这里插入图片描述

举报

相关推荐

0 条评论