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

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

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
/*************************************************************************
	> 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;
}
运行结果显示如下

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

03-01 11:36