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;
}
运行结果显示如下
相关推荐
颜酱14 小时前
队列练习系列:从基础到进阶的完整实现
javascript·后端·算法
用户57573033462414 小时前
两数之和:从 JSON 对象到 Map,大厂面试官到底在考察什么?
算法
程序猿追15 小时前
“马”上行动:手把手教你基于灵珠平台打造春节“全能数字管家”
算法
郑州光合科技余经理1 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1231 天前
matlab画图工具
开发语言·matlab
dustcell.1 天前
haproxy七层代理
java·开发语言·前端
norlan_jame1 天前
C-PHY与D-PHY差异
c语言·开发语言
ZPC82101 天前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC82101 天前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
琢磨先生David1 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode