日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

c++primer 4th edithon 第324页的一个练习题(9.43和9.44)

發(fā)布時(shí)間:2024/4/18 c/c++ 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++primer 4th edithon 第324页的一个练习题(9.43和9.44) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

下標(biāo)的方法:

9.43題

#include <iostream> #include <string> using namespace std; void replace_(string &s,const string &oldval,const string & newval) {for(size_t i = 0;i != s.size(); ++ i){if(s[i] == oldval[0]){string temp(s,i,oldval.size());cout << "Temp is:"<< temp << endl;if(temp == oldval){s = s.erase(i,oldval.size());s = s.insert(i,newval);cout << "old i is:" << i << endl;i = i + newval.size() - 1;cout << "new i is :"<<i <<endl << endl;if(i == s.size()){cout << "yes,break it"<<endl << endl;break;}}}} } int main() { string oldval{"tho"};string s{"asdfasthoasdfthoasdstho"},ss;string newval{"thouth"};replace_(s,oldval,newval);return 0; }

9.44題

迭代器的方法:

#include <iostream> #include <string> using namespace std; //replace oldval by newval object to s1 void replace_(string &s1,const string & oldval,const string & newval) {for(string::iterator it = s1.begin();it != s1.end() ; ++ it){if(*it == *oldval.begin()){string new_str(it,it + oldval.size());if(new_str == oldval){it = s1.erase(it,it+oldval.size());it = s1.insert(it,newval.begin(),newval.end());it += 5; }}} return ; } int main() {string s1 = "asdfasdfthoasdfthoasftho";string oldval = "tho";string newval = "though";unsigned cnt = 1;cout << s1 <<endl;replace_(s1,oldval,newval);cout << s1 <<endl;return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的c++primer 4th edithon 第324页的一个练习题(9.43和9.44)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。