欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

淺談C++中replace()方法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

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

用法一: 

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

運行結果:

用法二: 

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

運行結果:

用法三: 

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

運行結果:

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

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


運行結果:

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

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

運行結果:

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

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

運行結果:

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

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

運行結果:

用法八: 

/* 

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

運行結果:

用法九: 

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


運行結果:

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

上一篇:簡單分析C語言中指針數(shù)組與數(shù)組指針的區(qū)別

欄    目:C語言

下一篇:VC++中圖像處理類CBitmap的用法

本文標題:淺談C++中replace()方法

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2654.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有