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;
}
运行结果显示如下
相关推荐
橘颂TA2 分钟前
【剑斩OFFER】算法的暴力美学——力扣 227 题:基本计算机Ⅱ
c++·算法·leetcode·职场和发展·结构于算法
信奥卷王4 分钟前
2025年12月GESPC++二级真题解析(含视频)
算法
Z1Jxxx4 分钟前
输入n个数进行排序,要求先按奇偶后按从小到大的顺序排序
数据结构·算法
乐迪信息5 分钟前
乐迪信息:船体AI烟火检测,24小时火灾自动预警
人工智能·物联网·算法·目标检测·语音识别
Z1Jxxx6 分钟前
整除整除整除
开发语言·c++·算法
superman超哥8 分钟前
自定义迭代器的实现方法:深入Rust迭代器机制的核心
开发语言·后端·rust·编程语言·rust迭代器机制·自定义迭代器
dlpay9 分钟前
Visual Studio 2022中使用websocketpp
c++·ide·visual studio·boost·websocketpp
2501_921649499 分钟前
主流金融数据API对比:如何获取精准、及时的IPO数据
开发语言·python·金融·restful
云雾J视界10 分钟前
从Boost的设计哲学到工业实践:解锁下一代AI中间件架构的密码
c++·人工智能·中间件·架构·stackoverflow·boost
superman超哥11 分钟前
IntoIterator Trait的转换机制:解锁Rust迭代器生态的关键
开发语言·后端·rust·编程语言·rust trait·rust迭代器·trait转换机制