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

代码实现

for(auto v:J)时C++11中新特性,类似于java中的for each

#include<iostream>
#include<vector>
using namespace std;
int Agm(string J, string S)
{
    
    
	int cnt = 0;
	for (auto v : J)
	{
    
    
		cnt += count(S.begin(), S.end(), v);
	}
	return cnt;
}
int main()
{
    
    
	string J = "aA";
	string S = "aAAbbbb";
	cout << Agm(J, S);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43656233/article/details/107549826
今日推荐