【C++】统计string里面出现的字符的个数(使用count函数)
题目:给出一个string字符串,统计里面出现的字符的个数
解决方案:使用算法库里面的count函数,使用方法是count(begin,end,‘a’),其中begin指的是起始地址,end指的是结束地址,第三个参数指的是需要查找的字符。
#include <iostream>
#include <algotirhm>
#include <string>
using namespace std;
int main()
{
string temp = "aaabcdaaa!!!";
int num = count(temp.begin(),temp.end(),'a');
cout <<"在字符串" << temp << "中," <<"字母a出现的次数是" << num << endl;
return 0 ;
}
参考资料:http://www.cplusplus.com/reference/algorithm/count/?kw=count
应用题目:https://leetcode.com/problems/judge-route-circle/description/