C++中replace()?函數(shù)的基本用法
replace算法:
replace函數(shù)包含于頭文件#include<string>中。
泛型算法replace把隊(duì)列中與給定值相等的所有值替換為另一個(gè)值,整個(gè)隊(duì)列都被掃描,即此算法的各個(gè)版本都在
線性時(shí)間內(nèi)執(zhí)行———其復(fù)雜度為O(n)。
即replace的執(zhí)行要遍歷由區(qū)間[frist,last)限定的整個(gè)隊(duì)列,以把old_value替換成new_value。
下面說(shuō)下replace()的九種用法:(編譯軟件dev5.4.0)
用法一:用str替換指定字符串從起始位置pos開(kāi)始長(zhǎng)度為len的字符
string&?replace?(size_t?pos,?size_t?len,?const?string&?str);?
代碼如下:
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; str=str.replace(str.find("a"),2,"#"); //從第一個(gè)a位置開(kāi)始的兩個(gè)字符替換成# cout<<str<<endl; return 0; }
結(jié)果如下:
用法二:?用str替換?迭代器起始位置?和?結(jié)束位置?的字符
string&?replace?(const_iterator?i1,?const_iterator?i2,?const?string&?str);
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; str=str.replace(str.begin(),str.begin()+5,"#"); //用#替換從begin位置開(kāi)始的5個(gè)字符 cout<<str<<endl; return 0; }
結(jié)果如下:
用法三:?用substr的指定子串(給定起始位置和長(zhǎng)度)替換從指定位置上的字符串?
string&?replace?(size_t?pos,?size_t?len,?const?string&?str,?size_t?subpos,?size_t?sublen);
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; str=str.replace(str.begin(),str.begin()+5,"#"); //用#替換從begin位置開(kāi)始的5個(gè)字符 cout<<str<<endl; return 0; }
結(jié)果如下:
用法四:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
用str替換從指定位置0開(kāi)始長(zhǎng)度為5的字符串? ? ? ? ? ? ? ?
string&?replace(size_t?pos,?size_t?len,?const?char*?s);
代碼如下:
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; char * str1 = "12345"; str=str.replace(0,5,str1); //用str替換從指定位置開(kāi)始長(zhǎng)度為5的字符串 cout<<str<<endl; return 0; }
結(jié)果如下:
用法五:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
用str替換從指定迭代器位置的字符串? ? ? ? ? ? ? ?
string&?replace?(const_iterator?i1,?const_iterator?i2,?const?char*?s);?
代碼如下:
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; char * str1 = "12345"; str=str.replace(str.begin(),str.begin()+6,str1); //用str替換從指定迭代器位置的字符串 cout<<str<<endl; return 0; }
結(jié)果如下:
用法六:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
用s的前n個(gè)字符替換從開(kāi)始位置pos長(zhǎng)度為len的字符串? ? ? ? ? ? ? ? ?
string&?replace(size_t?pos,?size_t?len,?const?char*?s,?size_t?n);??
代碼如下:
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; char * str1 = "12345"; str=str.replace(0,6,str1,4); //用str1的前4個(gè)字符串替換從位置0~6的字符串 cout<<str<<endl; return 0; }
結(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);?
代碼如下:
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; char * str1 = "12345"; str = str.replace(str.begin(),str.begin()+6,str1,4); //用str1的前4個(gè)字符串替換從位置0~6的字符串 cout<<str<<endl; return 0; }
結(jié)果如下:
用法八:?用重復(fù)n次的c字符替換從指定位置pos長(zhǎng)度為len的內(nèi)容
string&?replace?(size_t?pos,?size_t?len,?size_t?n,?char?c);?
代碼如下:
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; char str1 = '#'; str = str.replace(0,6,3,str1); //用重復(fù)3次的str1字符替換的替換從位置0~6的字符串 cout<<str<<endl; return 0; }
結(jié)果如下:
用法九:?用重復(fù)n次的c字符替換從指定迭代器位置(從i1開(kāi)始到結(jié)束)的內(nèi)容?
string&?replace?(const_iterator?i1,?const_iterator?i2,?size_t?n,?char?c);?
代碼如下:
#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; char str1 = '#'; str = str.replace(str.begin(),str.begin()+6,3,str1); //用重復(fù)3次的str1字符替換的替換從指定迭代器位置的內(nèi)容 cout<<str<<endl; return 0; }
結(jié)果如下:
總結(jié)
到此這篇關(guān)于C++中replace()函數(shù)基本用法的文章就介紹到這了,更多相關(guān)C++?replace()?函數(shù)用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
理解C++編程中的std::function函數(shù)封裝
這篇文章主要介紹了理解C++編程中的std::function函數(shù)封裝,std::function是C++11標(biāo)準(zhǔn)中的新特性,需要的朋友可以參考下2016-04-04c語(yǔ)言實(shí)現(xiàn)含遞歸清場(chǎng)版掃雷游戲
掃雷大家應(yīng)該都玩過(guò),這是一個(gè)十分經(jīng)典的游戲,下面這篇文章主要給大家介紹了關(guān)于c語(yǔ)言實(shí)現(xiàn)含遞歸清場(chǎng)版掃雷游戲的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11C語(yǔ)言:利用指針編寫(xiě)程序,用梯形法計(jì)算給定的定積分實(shí)例
今天小編就為大家分享一篇C語(yǔ)言:利用指針編寫(xiě)程序,用梯形法計(jì)算給定的定積分實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12如何實(shí)現(xiàn)在C++中調(diào)用C函數(shù)
這篇文章主要介紹了如何實(shí)現(xiàn)在C++中調(diào)用C函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08