C++ //练习 10.2 重做上一题,但读取string序列存入list中。

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

练习 10.2 重做上一题,但读取string序列存入list中。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex10.2.cpp
	> Author: 
	> Mail: 
	> Created Time: Thu 29 Feb 2024 11:49:46 AM CST
 ************************************************************************/

#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<algorithm>
using namespace std;

int main(){
    list<string> lst;
    string str;
    
    cout<<"Enter strings: ";
    while(cin>>str){
        lst.push_back(str);
        if(cin.get() == '\n'){
            break;
        }
    }

    string val;
    cout<<"Enter value: ";
    cin>>val;

    int result;
    result = count(lst.begin(), lst.end(), val);
    if(result == 0){
        cout<<"The value is not in strings."<<endl;
    }
    else{
        cout<<"The value appears "<<result<<" times in strings."<<endl;
    }

    return 0;
}
运行结果显示如下
相关推荐
郝学胜_神的一滴3 分钟前
Effective Python 条款 2:遵循 PEP 8 编码风格,写出高质量 Python 代码
python·算法
郝学胜-神的一滴4 分钟前
Effective Python 条款 1:确认你正在使用的 Python 版本
开发语言·数据结构·python·程序人生·算法
sel_912 分钟前
【多轮对话论文导读(二)】多轮对话与LLM Agent论文阅读:长期记忆、多轮评估与Agent训练
人工智能·深度学习·算法·语言模型·自然语言处理
一木 之林30 分钟前
五、C++新特性、关键字与编译原理
java·jvm·算法
路多辛31 分钟前
全能型 Go Agent 框架 covonaut v1.0.8 发布:新增行内补全与多后端可观测性
开发语言·golang·agent
大模型丫丫33 分钟前
FastAPI 入门指南:从零开始构建高性能 Python API
开发语言·python·fastapi
汉克老师35 分钟前
CSP-J 初赛(以满分为目标):第四课《看懂计算机到底在算什么!——C++变量、表达式和运算符 》
c++·小学生·学c++编程
wuyk55544 分钟前
《WiFi 嵌入式物联网开发全套实战》| 第01章 WiFi整体架构详解:BSS/ESS/AP/STA模式彻底区分
开发语言·stm32·单片机·嵌入式硬件·mcu·物联网·51单片机
小小龙学IT1 小时前
zstd(Zstandard)开源压缩库实战指南:让数据又快又小
c++·开源
wuyk5551 小时前
第六章:CAN 调试工具、抓包分析、全套故障排查 + CAN-FD 进阶拓展
开发语言·stm32·单片机