国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

淺談C++中replace()方法

2020-05-23 14:13:13
字體:
來源:轉載
供稿:網友

C++編程語言中的string應用方式多樣化,每一種應用方式都能幫助我們提實現特定的功能需求。在這里我們將會為大家詳細介紹一下其中一個比較重要的用法,有關C++ replace()函數的應用方式,需要的朋友可以參考下

本文主要針對c++中常用replace函數用法給出九個樣例程序:

用法一:

 

 
  1. /* 
  2. *用str替換指定字符串從起始位置pos開始長度為len的字符  
  3. *string& replace (size_t pos, size_t len, const string& str);  
  4. */ 
  5. int main()  
  6. {  
  7. string line = "this@ is@ a test string!";  
  8. line = line.replace(line.find("@"), 1, ""); //從第一個@位置替換第一個@為空  
  9. cout << line << endl;  
  10. return 0;  
  11. }  

運行結果:

淺談C++中replace()方法

用法二:

 

 
  1. /* 
  2. *用str替換 迭代器起始位置 和 結束位置 的字符  
  3. *string& replace (const_iterator i1, const_iterator i2, const string& str);  
  4. */ 
  5. int main()  
  6. {  
  7. string line = "this@ is@ a test string!";  
  8. line = line.replace(line.begin(), line.begin()+6, ""); //用str替換從begin位置開始的6個字符  
  9. cout << line << endl;  
  10. return 0;  
  11. }  

運行結果:

淺談C++中replace()方法

用法三:

 

 
  1. /* 
  2. *用substr的指定子串(給定起始位置和長度)替換從指定位置上的字符串  
  3. *string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);  
  4. */ 
  5. int main()  
  6. {  
  7. string line = "this@ is@ a test string!";  
  8. string substr = "12345";  
  9. line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(從1位置數共3個字符)替換從0到5位置上的line  
  10. cout << line << endl;  
  11. return 0;  
  12. }  

運行結果:

淺談C++中replace()方法

用法四:

string轉char*時編譯器可能會報出警告,不建議這樣做

 

 
  1. /* 
  2. *用str替換從指定位置0開始長度為5的字符串  
  3. *string& replace(size_t pos, size_t len, const char* s);  
  4. */ 
  5. int main()  
  6. {  
  7. string line = "this@ is@ a test string!";  
  8. char* str = "12345";  
  9. line = line.replace(0, 5, str); //用str替換從指定位置0開始長度為5的字符串  
  10. cout << line << endl;  
  11. return 0;  
  12. }  

運行結果:

淺談C++中replace()方法

用法五:

string轉char*時編譯器可能會報出警告,不建議這樣做

 

 
  1. /* 
  2. *用str替換從指定迭代器位置的字符串  
  3. *string& replace (const_iterator i1, const_iterator i2, const char* s);  
  4. */ 
  5. int main()  
  6. {  
  7. string line = "this@ is@ a test string!";  
  8. char* str = "12345";  
  9. line = line.replace(line.begin(), line.begin()+9, str); //用str替換從指定迭代器位置的字符串  
  10. cout << line << endl;  
  11. return 0;  
  12. }  

運行結果:

淺談C++中replace()方法

用法六:

string轉char*時編譯器可能會報出警告,不建議這樣做

 

 
  1. /* 
  2. *用s的前n個字符替換從開始位置pos長度為len的字符串  
  3. *string& replace(size_t pos, size_t len, const char* s, size_t n);  
  4. */ 
  5. int main()  
  6. {  
  7. string line = "this@ is@ a test string!";  
  8. char* str = "12345";  
  9. line = line.replace(0, 9, str, 4); //用str的前4個字符替換從0位置開始長度為9的字符串  
  10. cout << line << endl;  
  11. return 0;  
  12. }  

運行結果:

淺談C++中replace()方法

用法七:

string轉char*時編譯器可能會報出警告,不建議這樣做

 

 
  1. /* 
  2. *用s的前n個字符替換指定迭代器位置(從i1到i2)的字符串  
  3. *string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);  
  4. */ 
  5. int main()  
  6. {  
  7. string line = "this@ is@ a test string!";  
  8. char* str = "12345";  
  9. line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4個字符替換指定迭代器位置的字符串  
  10. cout << line << endl;  
  11. return 0;  
  12. }  

運行結果:

淺談C++中replace()方法

用法八:

 

 
  1. /*  
  2.  
  3. *用重復n次的c字符替換從指定位置pos長度為len的內容  
  4. *string& replace (size_t pos, size_t len, size_t n, char c);  
  5. */ 
  6. int main()  
  7. {  
  8. string line = "this@ is@ a test string!";  
  9. char c = '1';  
  10. line = line.replace(0, 9, 3, c); //用重復3次的c字符替換從指定位置0長度為9的內容  
  11. cout << line << endl;  
  12. return 0;  
  13. }  

運行結果:

淺談C++中replace()方法

用法九:

 

 
  1. /* 
  2. *用重復n次的c字符替換從指定迭代器位置(從i1開始到結束)的內容  
  3. *string& replace (const_iterator i1, const_iterator i2, size_t n, char c);  
  4. */ 
  5. int main()  
  6. {  
  7. string line = "this@ is@ a test string!";  
  8. char c = '1';  
  9. line = line.replace(line.begin(), line.begin()+9, 3, c); //用重復3次的c字符替換從指定迭代器位置的內容  
  10. cout << line << endl;  
  11. return 0;  
  12. }  

運行結果:

淺談C++中replace()方法

注:所有使用迭代器類型的參數不限于string類型,可以為vector、list等其他類型迭代器。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松江区| 墨脱县| 鄂托克旗| 敦煌市| 抚远县| 眉山市| 砀山县| 闵行区| 江油市| 闽清县| 连云港市| 三明市| 昆山市| 巴南区| 遂宁市| 凤台县| 周宁县| 金秀| 连山| 三亚市| 定结县| 汉中市| 河池市| 云龙县| 遂川县| 高邮市| 定日县| 阳原县| 开封县| 南部县| 潮州市| 雷州市| 秭归县| 汕头市| 汝阳县| 黔东| 桐乡市| 惠水县| 汾西县| 景德镇市| 高清|