無題

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

          統計

          留言簿(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 閱讀(1405) 評論(0)  編輯  收藏 所屬分類: My Program

          主站蜘蛛池模板: 远安县| 仁寿县| 喀什市| 洛川县| 新邵县| 石屏县| 民和| 大名县| 嘉义县| 合川市| 喀喇| 邵东县| 晋江市| 新余市| 出国| 襄樊市| 武安市| 韶山市| 朝阳区| 惠来县| 台湾省| 全南县| 安多县| 搜索| 鸡西市| 龙川县| 鄂尔多斯市| 三亚市| 白朗县| 湄潭县| 于田县| 灵台县| 军事| 武清区| 环江| 施秉县| 双峰县| 莆田市| 涞源县| 麻江县| 如东县|