免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4086 | 回复: 9
打印 上一主题 下一主题

查找子串出现,请大家进来分析(已解决) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-24 22:29 |只看该作者 |倒序浏览
查找子串程序
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
        string b;
    int s=0,a;
        ifstream in("e:\\gf.cpp");

               
        while(getline(in,b))
        {        a=b.find("type");
       
                if(a>=0)
                        s=s+1;
        }
                        cout<<s;
                        return 0;
}

其中,gf.cpp文件的代码
#include<iostream>
#cnclude<cstring>
using namespace std;
class str_type{
        char string[80];
public:
        str_type(char*str=""){strcpy(string,str);}
        str_type operator+(str_type str);
        str_type operator+(char *str);
        void show_str(){cout<<string;}
};
str_type str_type::operator + (str_type str){
        str_type temp;
        strcpy(temp.string,string);
        strcat(temp.string,str.string);
        return temp;
}

我输入一个单词“char”时,查找数为正确的,输入子符串时,结果为错误的!这是什么原因呢,请大家多指教:em03::

[ 本帖最后由 yanglinayp 于 2006-4-27 21:46 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-04-26 22:07 |只看该作者

回复 1楼 yanglinayp 的帖子

是没看明白吗,怎么没人回复

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2006-04-26 22:16 |只看该作者
的确没怎么看明白,还有你的代码中那么多笑脸,晕。
发贴时下方有一个禁用 Smilies,把它勾上就好了。

论坛徽章:
0
4 [报告]
发表于 2006-04-27 13:49 |只看该作者
这下好了,我的意思就是find这个函数,为什么查找不出gf.cpp文件中type字串的个数?而查找char单词个数却可以,谢谢

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
5 [报告]
发表于 2006-04-27 13:54 |只看该作者
原帖由 yanglinayp 于 2006-4-27 13:49 发表
这下好了,我的意思就是find这个函数,为什么查找不出gf.cpp文件中type字串的个数?而查找char单词个数却可以,谢谢

什么叫char单词个数呀?

论坛徽章:
0
6 [报告]
发表于 2006-04-27 14:11 |只看该作者
while(getline(in,b))
        {        a=b.find("type");//如果换成char
        
                if(a>=0)
                        s=s+1;
        }

如果查找char的话呢,s统计的个数就是正确的,而查找type,s就不正确。

论坛徽章:
0
7 [报告]
发表于 2006-04-27 14:49 |只看该作者
不可能

论坛徽章:
0
8 [报告]
发表于 2006-04-27 15:04 |只看该作者
不信的话你把我的程序运行一下试试吧,我怀疑是不是在gf.cpp中type是包含在str_type 中的原因,要是char的话它是一个独立的单词?请讲解一下好吗?

论坛徽章:
0
9 [报告]
发表于 2006-04-27 15:34 |只看该作者

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>

  4. using namespace std;

  5. int main(){
  6.         string b;
  7.     int s=0,a;
  8.         ifstream in("gf.cpp");       
  9.         string substr="type";

  10.         while(getline(in,b)) {        
  11.                 a=b.find(substr);
  12.                
  13.                 while(a!=string::npos) {
  14.                         s=s+1;
  15.                         a=b.find(substr,a+substr.length());
  16.                 }
  17.         }
  18.         cout<<s<<endl;
  19.         return 0;
  20. }
复制代码

论坛徽章:
0
10 [报告]
发表于 2006-04-27 21:23 |只看该作者

回复 9楼 tyc611 的帖子

谢谢楼上的,我知道我错在哪里了,不是find()问题,是我没有控制好查找次数,每行只查找到第一个符合条件的它就不再继续查找了,所以总数就不对
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP