淺談C++中replace()方法
本文主要針對(duì)c++中常用replace函數(shù)用法給出九個(gè)樣例程序:
用法一:
/*
*用str替換指定字符串從起始位置pos開始長(zhǎng)度為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, ""); //從第一個(gè)@位置替換第一個(gè)@為空
cout << line << endl;
return 0;
}
運(yùn)行結(jié)果:
用法二:
/*
*用str替換 迭代器起始位置 和 結(jié)束位置 的字符
*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個(gè)字符
cout << line << endl;
return 0;
}
運(yùn)行結(jié)果:
用法三:
/*
*用substr的指定子串(給定起始位置和長(zhǎng)度)替換從指定位置上的字符串
*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個(gè)字符)替換從0到5位置上的line
cout << line << endl;
return 0;
}
運(yùn)行結(jié)果:
用法四:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
/*
*用str替換從指定位置0開始長(zhǎng)度為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開始長(zhǎng)度為5的字符串
cout << line << endl;
return 0;
}
運(yùn)行結(jié)果:
用法五:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
/*
*用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;
}
運(yùn)行結(jié)果:
用法六:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
/*
*用s的前n個(gè)字符替換從開始位置pos長(zhǎng)度為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個(gè)字符替換從0位置開始長(zhǎng)度為9的字符串
cout << line << endl;
return 0;
}
運(yùn)行結(jié)果:
用法七:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
/*
*用s的前n個(gè)字符替換指定迭代器位置(從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個(gè)字符替換指定迭代器位置的字符串
cout << line << endl;
return 0;
}
運(yùn)行結(jié)果:
用法八:
/*
*用重復(fù)n次的c字符替換從指定位置pos長(zhǎng)度為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); //用重復(fù)3次的c字符替換從指定位置0長(zhǎng)度為9的內(nèi)容
cout << line << endl;
return 0;
}
運(yùn)行結(jié)果:
用法九:
/*
*用重復(fù)n次的c字符替換從指定迭代器位置(從i1開始到結(jié)束)的內(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); //用重復(fù)3次的c字符替換從指定迭代器位置的內(nèi)容
cout << line << endl;
return 0;
}
運(yùn)行結(jié)果:
注:所有使用迭代器類型的參數(shù)不限于string類型,可以為vector、list等其他類型迭代器。
上一篇:簡(jiǎn)單分析C語言中指針數(shù)組與數(shù)組指針的區(qū)別
欄 目:C語言
本文標(biāo)題:淺談C++中replace()方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2654.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery


