功能
向函數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;
}