
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个输出结果是怎么得来的,谢谢! 展开
#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个输出结果是怎么得来的,谢谢! 展开
展开全部
;//从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
挺正常的
int rfind(const string &s,int pos = npos) const;
你的第一个从 第10个符号开始,向前找 "make" ,因为没有,所以直接返回 -1
你的第二个从 第40个符号开始, 向前找"make" ,返回 15
你的第三个从 整个字符串的最想向前找 "day",所以返回 3
挺正常的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询