分析
一开始还想着用stringstream,后来发现直接用s.find(),效率极高!
C++ 代码
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
int i=1,fir=0,cnt=0;
while(getline(cin,s),s!=".")
{
int id=s.find("chi1 huo3 guo1"); //查找"chi1 huo3 guo1"在字符串中的位置
if(id!=-1) //如果找到了
{
if(!fir) fir=i; //确定第一次出现的位置
cnt++;
}
i++;
}
cout<<i-1<<endl;
if(cnt){
cout<<fir<<" "<<cnt;}
else puts("-_-#");
return 0;
}
C++字符串查找实践
本文介绍了一种使用C++进行字符串查找的方法,通过`s.find()`函数高效定位特定字符串出现的位置,并记录其首次出现及总次数。
3389

被折叠的 条评论
为什么被折叠?



