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;
}
运行结果显示如下
相关推荐
xueyongfu4 分钟前
从Diffusion到VLA pi0(π0)
人工智能·算法·stable diffusion
爱装代码的小瓶子8 分钟前
【c++进阶】从C++98到C++11的奇妙旅程(故事科普版)
开发语言·c++
智航GIS10 分钟前
2.3 运算符详解
开发语言·python
永远睡不够的入14 分钟前
快排(非递归)和归并的实现
数据结构·算法·深度优先
cheems952714 分钟前
二叉树深搜算法练习(一)
数据结构·算法
web3.088899915 分钟前
接入API-自动化批量获取淘宝商品详情数据
开发语言·python
sin_hielo17 分钟前
leetcode 3074
数据结构·算法·leetcode
Yzzz-F23 分钟前
算法竞赛进阶指南 动态规划 背包
算法·动态规划
程序员-King.26 分钟前
day124—二分查找—最小化数组中的最大值(LeetCode-2439)
算法·leetcode·二分查找
predawnlove31 分钟前
【NCCL】4 AllGather-PAT算法
算法·gpu·nccl