【C++】统计string里面出现的字符的个数(使用count函数)

题目:给出一个string字符串,统计里面出现的字符的个数

解决方案:使用算法库<algorithm>里面的count函数(不是s.count()!!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 ;
}

 

posted @ 2022-10-15 17:51  算法扫地僧  阅读(1171)  评论(0编辑  收藏  举报