关于函数find问题

墓后煮屎人 2010-12-20 04:11:59

string str("ABCDSABCS");//原串

int nPos=0;
cout<<endl;
cout<<"=======能根据目标串在源串查找,并返回其起始位置======="<<endl;
cout<<str.find("B")<<endl;

这个返回一个“1”;没有问题。

然后我让他查找两个就又问题了。
cout<<"=======如果找到多个,依次返回它们的起始位置======="<<endl;
while(1)
{

nPos=str.find("BC",nPos)+1;
if (nPos-1==string::npos)
break;
cout<<nPos<<endl;
}
怎么是“2”,“7”,不应该是“1”,“6”吗。。不解。。。

...全文
129 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
healer_kx 2010-12-20
  • 打赏
  • 举报
回复
是得BS。。。
龙哥依旧 2010-12-20
  • 打赏
  • 举报
回复
鄙视楼主一下!
墓后煮屎人 2010-12-20
  • 打赏
  • 举报
回复
の~~怎么又好了。。。多谢多谢。。。
就想叫yoko 2010-12-20
  • 打赏
  • 举报
回复
把 if (nPos-1==string::npos)
改成if (nPos == string::npos)就不用+1了
maoxing63570 2010-12-20
  • 打赏
  • 举报
回复

while(1)
{

nPos=str.find("BC",nPos);
if (nPos==string::npos)
break;
cout<<nPos++<<endl;
}

无代码,无真相
墓后煮屎人 2010-12-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 maoxing63570 的回复:]
C/C++ code


while(1)
{

nPos=str.find("BC",nPos);
if (nPos-1==string::npos)
break;
cout<<nPos++<<endl;
}


cout<<"how about now?"
[/Quote]


更是死循环。。。有没有新的办法啊。。
maoxing63570 2010-12-20
  • 打赏
  • 举报
回复


while(1)
{

nPos=str.find("BC",nPos);
if (nPos-1==string::npos)
break;
cout<<nPos++<<endl;
}

cout<<"how about now?"
墓后煮屎人 2010-12-20
  • 打赏
  • 举报
回复
可是不加1就死循环了啊。。。。。
hai040 2010-12-20
  • 打赏
  • 举报
回复
find("BC",nPos+1);
liuintermilan 2010-12-20
  • 打赏
  • 举报
回复
nPos=str.find("BC",nPos)+1;
你不是加一了么。。。
pengzhixi 2010-12-20
  • 打赏
  • 举报
回复
nPos=str.find("BC",nPos)+1;//你不是+1了吗
healer_kx 2010-12-20
  • 打赏
  • 举报
回复
str.find("BC",nPos)+1 ,你自己给 + 1 了。。。

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧