無題

          拿個學位是騙自己的。學問是一輩子的。

          統計

          留言簿(3)

          閱讀排行榜

          評論排行榜

          統計給定字符串在指定字符串中出現的次數(C++源代碼)

           1//#include <iostream>
           2//統計substr在str中出現的次數并返回
           3int CountSubString(std::string const& str, std::string const& substr){
           4  int nCount = 0;
           5  std::string::size_type substrSize = substr.size();
           6  std::string::size_type IndexOfFind = 0;
           7  IndexOfFind = str.find(substr, IndexOfFind);
           8  while (IndexOfFind!=std::string::npos) {
           9    ++nCount;
          10    if (substrSize>1){
          11      IndexOfFind+=substrSize;
          12    }

          13    else{
          14      ++IndexOfFind;
          15    }

          16    IndexOfFind = str.find(substr, IndexOfFind);
          17  }

          18  return nCount;
          19}
          這是今天參考網上一個人的代碼修改完善的。

          posted on 2007-12-05 18:26 閱讀(1410) 評論(0)  編輯  收藏 所屬分類: My Program

          主站蜘蛛池模板: 岐山县| 昭平县| 平度市| 葫芦岛市| 叶城县| 策勒县| 衢州市| 金门县| 罗城| 黎平县| 满城县| 巴楚县| 左贡县| 个旧市| 泗阳县| 桂东县| 荥阳市| 云梦县| 肇东市| 镇赉县| 澄江县| 海阳市| 尚义县| 九江市| 平凉市| 扎赉特旗| 司法| 墨竹工卡县| 珲春市| 贞丰县| 乌拉特前旗| 奇台县| 潮安县| 中西区| 乐都县| 河北省| 会泽县| 会东县| 鹤峰县| 洪雅县| 丰顺县|