溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++中replace()函數(shù)怎么使用

發(fā)布時間:2021-11-29 09:18:21 來源:億速云 閱讀:255 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“C++中replace()函數(shù)怎么使用”,在日常操作中,相信很多人在C++中replace()函數(shù)怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++中replace()函數(shù)怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

replace算法:

replace函數(shù)包含于頭文件#include<string>中。

泛型算法replace把隊列中與給定值相等的所有值替換為另一個值,整個隊列都被掃描,即此算法的各個版本都在

線性時間內(nèi)執(zhí)行———其復(fù)雜度為O(n)。

即replace的執(zhí)行要遍歷由區(qū)間[frist,last)限定的整個隊列,以把old_value替換成new_value。

下面說下replace()的九種用法:(編譯軟件dev5.4.0)

用法一:用str替換指定字符串從起始位置pos開始長度為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,"#");  //從第一個a位置開始的兩個字符替換成#
	cout<<str<<endl; 
	return 0;
}

結(jié)果如下:

C++中replace()函數(shù)怎么使用

用法二: 用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位置開始的5個字符
	 cout<<str<<endl;
	 return 0; 
}

結(jié)果如下:

C++中replace()函數(shù)怎么使用

用法三: 用substr的指定子串(給定起始位置和長度)替換從指定位置上的字符串 

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é)果如下:

C++中replace()函數(shù)怎么使用

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

用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é)果如下:

C++中replace()函數(shù)怎么使用

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

用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é)果如下:

C++中replace()函數(shù)怎么使用

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

用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é)果如下:

C++中replace()函數(shù)怎么使用

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

用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é)果如下:

C++中replace()函數(shù)怎么使用

用法八: 用重復(fù)n次的c字符替換從指定位置pos長度為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é)果如下:

C++中replace()函數(shù)怎么使用

用法九: 用重復(fù)n次的c字符替換從指定迭代器位置(從i1開始到結(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é)果如下:

C++中replace()函數(shù)怎么使用

到此,關(guān)于“C++中replace()函數(shù)怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(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)容。

AI