统计字符串中某个字符出现的次数

来源:互联网 发布:读书笔记 软件 编辑:程序博客网 时间:2024/05/06 07:55

统计字符串中某个字符出现的次数

用标准库算法函数“ count_if”。


1、标准库string类型

const stringstr = “testString”;

int count = count_if( str.begin(), str.end(), bind2nd(equal_to<char>(), '某字符);


2、CString类型

CString str = “testString”;

int count = count_if( (LPCTSTR)str, (LPCTSTR)str+str.GetLength(), bind2nd(equal_to<TCHAR>(), _T('某字符')) );
原创粉丝点击