string字符串的find以及replace综合
string字符串的find以及replace综合
将str里的所有子串s用t换
#include<bits/stdc++.h>
using namespace std;
int main(){
string a,b,c;
cin>>a>>b>>c;
int pos=a.find(b,0);
while(pos!=-1){//找子串
a.replace(pos,b.length(),c);
pos=a.find(b,pos+c.length());
}
cout<<a;
return 0;
}
总结:
1.对于所有子串问题常用pos然后循环!=-1
2.replace函数
原字符串.replace(替换起点,要替换的原长度,替换内容字符串)

浙公网安备 33010602011771号