功能
向函数fun中传入三个参数:将s中所有oldval替换为newval
代码
#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以查找oldvals.replace(p, oldval.size(), newval);// 找到以后替换为newvalp += 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;return 0;
}