您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++中replace()函數(shù)怎么使用”,在日常操作中,相信很多人在C++中replace()函數(shù)怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++中replace()函數(shù)怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
replace函數(shù)包含于頭文件#include<string>中。
泛型算法replace把隊列中與給定值相等的所有值替換為另一個值,整個隊列都被掃描,即此算法的各個版本都在
線性時間內(nèi)執(zhí)行———其復(fù)雜度為O(n)。
即replace的執(zhí)行要遍歷由區(qū)間[frist,last)限定的整個隊列,以把old_value替換成new_value。
下面說下replace()的九種用法:(編譯軟件dev5.4.0)
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,"#"); //從第一個a位置開始的兩個字符替換成# cout<<str<<endl; return 0; }
結(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位置開始的5個字符 cout<<str<<endl; return 0; }
結(jié)果如下:
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位置開始的5個字符 cout<<str<<endl; return 0; }
結(jié)果如下:
用str替換從指定位置0開始長度為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替換從指定位置開始長度為5的字符串 cout<<str<<endl; return 0; }
結(jié)果如下:
用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é)果如下:
用s的前n個字符替換從開始位置pos長度為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個字符串替換從位置0~6的字符串 cout<<str<<endl; return 0; }
結(jié)果如下:
用s的前n個字符替換指定迭代器位置(從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個字符串替換從位置0~6的字符串 cout<<str<<endl; return 0; }
結(jié)果如下:
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é)果如下:
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é)果如下:
到此,關(guān)于“C++中replace()函數(shù)怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。