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;
}
运行结果显示如下
相关推荐
8Qi814 小时前
回文子串(Palindromic Substrings)—— 题解
算法·leetcode·职场和发展·动态规划
想吃火锅100517 小时前
【leetcode】405.数字转换为十六进制数js
开发语言·javascript·ecmascript
专注VB编程开发20年17 小时前
AI 生成C# WinForm 窗体 = 目前就是垃圾
开发语言·人工智能·c#
cfm_291417 小时前
JVM GC垃圾回收初步了解
java·开发语言·jvm
~小先生~18 小时前
Python从入门到放弃(一)
开发语言·python
许彰午18 小时前
17_synchronized关键字深度解析
java·开发语言
z落落18 小时前
C# 泛型接口和泛型类+泛型约束
开发语言·c#
阿正的梦工坊18 小时前
【Rust】02-变量、不可变性与基础类型
开发语言·后端·rust
阿正的梦工坊18 小时前
【Rust】08-集合类型、字符串与迭代器入门
开发语言·rust·c#