替换string中的部分字符功能
向函数fun中传⼊三个参数:将s中所有oldval替换为newval
代码
c++中string的用法#include<iostream>
#include<list>
#include<deque>
#include<vector>
#include<forward_list>
#include<array>
using namespace std;
void fun(string &s,const string &oldval,const string &newval){
int p =0;// 匹配成功位置的下标
while((p = s.find(oldval, p))!=-1){
// 遍历s以查oldval
// 到以后替换为newval
p += newval.size();
// 调整下标,略过替换的newval
}
}
int main(int argc,char const*argv[]){
string s ="hello tho thru th thr tho thru";
fun(s,"thog","though");
fun(s,"thru","through");
cout << s << endl;
return0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。