C++ primer 第五版 中文版 练习 10.2
题目:重做上一题,但读取string序列存入list中。
上一题题目为:头文件algorithm中定义了一个名为conunt的函数,它类似find,接受一对迭代器和一个值作为参数。
count返回给定值在序列中出现的次数。编写程序,读取int序列存入vector中,打印有多少个元素的值等于给定的值。
答:
/*
重做上一题,但读取string序列存入list中。
上一题题目为:头文件algorithm中定义了一个名为conunt的函数,它类似find,接受一对迭代器和一个值作为参数。
count返回给定值在序列中出现的次数。编写程序,读取int序列存入vector中,打印有多少个元素的值等于给定的值。
*/
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
list<string> slist = { "my", "love", "C++", "love", "primer", "is", "hard", "hard", "hard" };
string str = "love";
auto cnt = count(slist.begin(), slist.end(), str);
cout << str << "在slist中出现的次数为:" << cnt << endl;
return 0;
}