C++string的查找,find()函数有关问题

#include<iostream>#include<string>usingnamespacestd;voidmain(){strings("Studyhardandm... #include<iostream>
#include<string>
using namespace std;
void main()
{
string s("Study hard and make progress every day!every day!!");
int loc;
loc=s.rfind("make",10);
cout<<"The word make is at index "<<loc<<endl;
loc=s.rfind("make",40);
cout<<"the word is at index"<<loc<<endl;
loc=s.find_first_of("day");
cout<<"the word day(first) is at index"<<loc<<endl;
}
输出结果:The word make is at index -1
the word is at index15
the word day(first) is at index3
求分析这3个输出结果是怎么得来的,谢谢!
展开
 我来答
mafangsan
2014-01-07 · TA获得超过1.2万个赞
知道大有可为答主
回答量:1万
采纳率:71%
帮助的人:2752万
展开全部
;//从pos开始从后向前查找字符c在当前串中的位置int rfind(const char *s, int pos = npos) const;
int rfind(const string &s,int pos = npos) const;

你的第一个从 第10个符号开始,向前找 "make" ,因为没有,所以直接返回 -1
你的第二个从 第40个符号开始, 向前找"make" ,返回 15
你的第三个从 整个字符串的最想向前找 "day",所以返回 3

挺正常的
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式