#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
int main()
{
string s;
string s1;
cin>>s>>s1;
s.find(s1);//返回s1在s中的位置,没找到返回-1
s.find_first_of(s1);//返回任意字符s1在s中第一次出现的位置,s1是字符不可以为字符串
s.find(s1, a);//从s下标为a开始查找字符串s1,返回起始位置,找不到返回-1
string s="abb abb abb"; //查找字符s1在s中出现的所有起始位置
string s1="a"; //char s1='a';
int position=0;
int i=0;
while((position=s.find_first_of(s1,position))!=string::npos)
{
cout<<position<<endl;
position++;
i++;//i为出现的总次数
}
cout<<"total="<<i<<endl;
/*0
4
8
total = 3
*/
string s="aaabcd"; //查找s1与s第一次出现不符合的位置
string s1="aaafcd";
int position;
position=s.find_first_not_of(s1);
cout<<position<<endl;
/*
3
*/
string str ("The sixth sick sheik's sixth sheep's sick.");
string key ("sixth");
size_t found;
int found = str.rfind(key); //由于是rfind, 所以得到的是后面的sixth的位置, 如果使用find, 得到的就是前一个sixth的位置
return 0;
}
c ++ string.find() 用法
最新推荐文章于 2025-05-23 22:07:38 发布