金海佳学C++primer 练习9.44
生活随笔
收集整理的這篇文章主要介紹了
金海佳学C++primer 练习9.44
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
string中替換oldVal->newVal(using replace)
Practice 9.44
#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;void old2new_2(string & s, string oldVal, string newVal) {int len = (int)s.length();int len2 = (int)oldVal.length();for(int i = 0; i < len; i++) {int cnt = 0;if(s[i] == oldVal[cnt]) {int j = 0;for(j = 0; i+j<len && cnt<len2; j++, cnt++) {if(s[i+j] != oldVal[cnt]) break;}if(j == len2) {s.replace(i,len2,newVal);}}} }int main() {string s, oldVal, newVal;cin >> s >> oldVal >> newVal;old2new_2(s,oldVal,newVal);cout << "s: " << s << endl;cout << "old: " << oldVal << endl;cout << "new: " << newVal << endl;cout << s << endl; }Output
s: jinwanglangjia old: hai new: wanglang jinwanglangjia今天舞臺上的你美到讓我心跳加速。
總結
以上是生活随笔為你收集整理的金海佳学C++primer 练习9.44的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编程语言中,还有一种语言,那就是用中文来
- 下一篇: 张士峰:经验分享之我的科研历程