takeiteasy 发表于 2014-6-16 17:37:07

查找CString里面出现某个字符的次数

1、Replace函数替换查找

Replace函数返回值:返回被替换的字符数。如果这个字符串没有改变则返回零。

CString sTest="aabbccaadd";
int nCount=s.Replace("a","a");

nCount就是你的想要的值

CString::Replace
int Replace( TCHAR chOld, TCHAR chNew );
int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );

Return Value
The number of replaced instances of the character. Zero if the string isn't changed.


2、标准函数 count_if

#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;

int main( void )
{
    const string a = " 12113";
    cout << count_if( a.begin(), a.end(), bind2nd(equal_to<char>(),'1') ) << endl;
    return 0;
}

CString也一样,但它没有标准的迭代器,因此需要写成
count_if( (LPCTSTR)a, (LPCTSTR)a+a.GetLength(), bind2nd(equal_to<TCHAR>(),_T('某字符')) )
页: [1]
查看完整版本: 查找CString里面出现某个字符的次数