金海佳学C++primer 练习9.43
生活随笔
收集整理的這篇文章主要介紹了
金海佳学C++primer 练习9.43
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
string中替換oldVal->newVal
Practice 9.43
#include <iostream> #include <queue> #include <string> #include <vector> #include <algorithm> #include <list> #include <iterator> #include <cmath> #include <cstring> #include <forward_list> #include <sstream> using namespace std;bool ok(string::iterator it1, string s2) {auto it2 = s2.begin();while(it2 != s2.end()) {if(*it1 != *it2) break;it1++, it2++;}if(it2 == s2.end()) return true;else return false; }void old2new(string & s, string oldVal, string newVal) {auto it = s.begin();int len_old = (int)oldVal.length();while(it != s.end()) {if(ok(it, oldVal)) {s.erase(it,it+len_old);s.insert(it,newVal.begin(),newVal.end()); }else {++it;}} }int main() {string s, oldVal, newVal;cin >> s >> oldVal >> newVal;old2new(s,oldVal,newVal);cout << "s: " << s << endl;cout << "old: " << oldVal << endl;cout << "new: " << newVal << endl;cout << s << endl;return 0; }Ouput
s: jinwanglangjia old: hai new: wanglang jinwanglangjiaThe world is his who enjoys it.
總結
以上是生活随笔為你收集整理的金海佳学C++primer 练习9.43的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动驾驶高精地图调研
- 下一篇: s3c2440移植MQTT