#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
/*
T:要搜索的串
P:要匹配的串
*/
int pos(string T, string P, int n){
if(n==0)return -1;//相当于没找
int count = 0;
unsigned begined = 0;
while((begined=T.find(P,begined))!=string::npos){
count++;
begined += P.length();
if(n==count){
return begined-1;
}
}
return -2;//意味着没有那么多次
}
int main(){
int n = 2;
string str1 = "mamad";
string str2 = "m";
cout<<str2<<" 第 "<<n<<" 次出现的位置是 "<<pos(str1, str2, n)<<endl;
return 0;
}
C++ string 找出字符第n次出现的位置
最新推荐文章于 2023-06-09 17:43:20 发布