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;
}
运行结果显示如下
相关推荐
scx201310041 小时前
20251214 字典树总结
算法·字典树
leiming61 小时前
MobileNetV4 (MNv4)
开发语言·算法
llxxyy卢1 小时前
反序列化之PHP
开发语言·php
雨落在了我的手上1 小时前
C语言入门(三十一):预处理详解(1)
c语言·开发语言
BD_Marathon1 小时前
关于JS和TS选择的问题
开发语言·javascript·ecmascript
YGGP1 小时前
【Golang】LeetCode 136. 只出现一次的数字
算法·leetcode
YGGP2 小时前
【Golang】LeetCode 169. 多数元素
算法·leetcode
MC皮蛋侠客2 小时前
distcc结合VSCode实现分布式编译的全面指南
c++·ide·分布式·vscode
顾安r2 小时前
11.20 脚本网页 数学分支
算法·数学建模·html