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;
}
运行结果显示如下
相关推荐
Wang's Blog11 小时前
Java 项目实战: 交友项目 - FastDFS:入门案例
java·开发语言·交友
apcipot_rain12 小时前
C++转Java日记(03)
java·开发语言·c++
布莱克60512 小时前
C++ 中 map 的 insert 与 emplace:key 已存在时会覆盖吗?
开发语言·数据结构·c++·算法·map
xys_67813 小时前
深挖C++:类和对象(中)
开发语言·c++
灿飞屋13 小时前
Rust 编程其实很简单
开发语言·rust
MOONICK15 小时前
QT元数据
开发语言·qt
D e re k ZH15 小时前
C++ 系统学习手记:从语法细节到工程实践
开发语言·数据结构·c++·算法
灿飞屋16 小时前
Kotlin与Java虚拟机三:控制流
android·java·开发语言·后端·python·kotlin·安卓
石小石Orz16 小时前
AI 正在蚕食我们的编码能力
前端·后端·算法