C++ //练习 9.27 编写程序,查找并删除forward_list<int>中的奇数元素。

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;
}
运行结果显示如下
相关推荐
致Great4 小时前
Pi 的上下文压缩,到底是怎么工作的?
算法
必须会一定会5 小时前
Agent Plugins 1.0实战:plugin.json、skills、mcp.json目录结构与迁移
开发语言·人工智能·ai编程
St_rive5 小时前
Page Object设计模式
java·开发语言·设计模式
wp123_15 小时前
硬件元器件笔记|IPX8 防水 Type‑C 母座安费诺 124018802112A 与 TONEVEE TY48086‑24A 分析
c语言·开发语言·笔记
wuyk5555 小时前
4.树:一对多的层次数据结构
开发语言·数据结构·stm32·单片机
watersink5 小时前
机器学习聚类算法
算法·机器学习·聚类
luj_17686 小时前
桥牌思维启示:系统设计的模块化架构
c语言·开发语言·c++·经验分享·算法
会周易的程序员7 小时前
aiDgePLC iec61131 虚拟机 完整使用文档
c++·物联网·架构·st·iec61131