統計字符串中某個字符出現的次數

統計字符串中某個字符出現的次數

用標準庫算法函數“ 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('某字符')) );
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章