代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s="follow world";
cout<<"字符串为:"<<s<<endl;
// ---------------------------------------------------------------------
cout<<"----------------------------------"<<endl<<endl;
//----------------------------------------------------------------------
//1: s.find("string");
//返回字母在字符串中的位置
//若不存在 则返回npos || end()
cout<<"world第一次出现的下标为: "<<s.find("world")<<endl;
cout<<"world是第几个字符: "<<s.find("world")+1<<endl;
// ---------------------------------------------------------------------
cout<<"----------------------------------"<<endl<<endl;
//----------------------------------------------------------------------
//2: s.find_first_of("string") && s.find_last_of("string")
//返回子串在母串中首次出现的位置,何最后一次出现的位置
cout<<"w首次出现的位置:"<<s.find_first_of("w")<<endl;
cout<<"w最后出现的位置:"<<s.find_last_of("w")<<endl;
// ---------------------------------------------------------------------
cout<<"----------------------------------"<<endl<<endl;
//----------------------------------------------------------------------
//3:s.find("string",int);
//查找某一给定位置后的字串的位置
cout<<"下标 6(第一个w)后开始出现w的位置:"<<s.find("w",6)<<endl;
// ---------------------------------------------------------------------
cout<<"----------------------------------"<<endl<<endl;
//----------------------------------------------------------------------
//4:while(index=s.find("w",index)!=s.npos)
//查找所有字串在母串中的位置
int i=0,index=0;
while((index=s.find("w",index))!=string::npos)
{
i++;
cout<<"第"<<i<<"个w出现的位置:"<<index++<<endl;
}
// ---------------------------------------------------------------------
cout<<"----------------------------------"<<endl<<endl;
//----------------------------------------------------------------------
//5:s.rfind("string");
//反向查找子串在母串中出现的位置
//若正向查找与反向查找的位置不相同 说明位置补唯一
cout<<"倒数第几个字符是w:" <<s.rfind("w")<<endl;
cout<<"----"<<endl;
cout<<"字符串为:"<<s<<endl;
cout<<"此时字符串中w"<<(s.find("w")==s.rfind("w")?"唯一":"不唯一")<<endl;
cout<<"----"<<endl;
s="follow";
cout<<"字符串为:"<<s<<endl;
cout<<"此时字符串中w"<<(s.find("w")==s.rfind("w")?"唯一":"不唯一");
}
运行结果
find函数在做题中的实践
例一:2020天梯赛 L1-6 吃火锅
这种天气你有什么破事打电话给我基本没用。
但是如果你说“吃火锅”,那就厉害了,我们的故事就开始了。
本题要求你实现一个程序,
自动检查你朋友给你发来的信息里有没有 chi1 huo3 guo1。
输入格式:
输入每行给出一句不超过 80 个字符的、以回车结尾的朋友信息,
信息为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。
当读到某一行只有一个英文句点 . 时,输入结束,
此行不算在朋友信息里。
输出格式:
首先在一行中输出朋友信息的总条数。
然后对朋友的每一行信息,
检查其中是否包含 chi1 huo3 guo1,
并且统计这样厉害的信息有多少条。
在第二行中首先输出第一次出现 chi1 huo3 guo1 的信息是第几条
(从 1 开始计数),然后输出这类信息的总条数,其间以一个空格分隔。
题目保证输出的所有数字不超过 100。
如果朋友从头到尾都没提 chi1 huo3 guo1 这个关键词,
则在第二行输出一个表情 -_-#。
输入样例 1:
Hello!
are you there?
wantta chi1 huo3 guo1?
that's so li hai le
our story begins from chi1 huo3 guo1 le
.
输出样例 1:
5
3 2
输入样例 2:
Hello!
are you there?
wantta qi huo3 guo1 chi1huo3guo1?
that's so li hai le
our story begins from ci1 huo4 guo2 le
.
输出样例 2:
5
-_-#
#include<iostream>
#include<cstring>
using namespace std;
int ans,ind,flag;
int main()
{
string s;
while(getline(cin,s)&&s!=".")
{
ind++;
if(s.find("chi1 huo3 guo1")!=s.npos)
{
if(!flag) flag=ind;
ans++;
}
}
cout<<ind<<endl;
if(!flag) cout<<"-_-#";
else cout<<flag<<" "<<ans;
return 0;
}
例二:给出一个字符串,串中会出现有人名,找到一个只有一个人名的字符串。
#include <bits/stdc++.h>
using namespace std;
vector<string> s;
int main()
{
s.push_back("Danil");
s.push_back("Olya");
s.push_back("Slava");
s.push_back("Ann");
s.push_back("Nikita");///建立动态数组
string a;
cin>>a;
int res = 0;
for(int i = 0; i < 5; i++)
{
if(a.find(s[i]) != a.npos)
{
res++;
if(a.rfind(s[i]) != a.find(s[i]))///一个字符中出现多个一样的名字
{
res++;
}
}
}
if(res == 1)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
return 0;
}
例三:你有n个字符串。 每个字符串由小写英文字母组成。 重新排序给定的字符串,使得对于每个字符串,在它之前的所有字符串都是它的子串。
#include<string>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
bool cmp(string a, string b)
{
if (a.length() == b.length())
return a < b;
return a.length() < b.length();
}
int main()
{
int n;
string s[111];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
cin >> s[i];
}
sort(s, s + n, cmp);
int flag = 1;
for (int i = 1; i < n; i++)
{
if (s[i].find(s[i-1]) == string::npos)
{
flag = 0;
break;
}
}
if (flag)
{
cout << "YES" << endl;
for (int i = 0; i < n; i++)
{
cout << s[i] << endl;
}
}
else
{
cout << "NO" << endl;
}
return 0;
}
例四:查询区间内子串在母串中的个数。
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m,q,i,j,l,r,len;
int counts;
int vis[10010];
string s1,s2;
cin>>n>>m>>q;
cin>>s1>>s2;
len=s2.size();
memset(vis,0,sizeof(vis));
string::size_type pos=0;
while((pos=s1.find(s2,pos))!=string::npos)
{
vis[pos+1]=pos+1;
pos++;
}
for(i=1;i<=q;i++)
{
counts=0;
scanf("%d%d",&l,&r);
for(j=l;j<=r;j++)
{
if(vis[j]!=0&&vis[j]+len-1<=r)
{
counts++;
}
}
printf("%d\n",counts);
}
return 0;
}
参考:https://www.cnblogs.com/wkfvawl/p/9429128.html 原作者:王陸
例题2-4为搬运。