C++ replace() 函數用法

replace算法:

                replace函數包含於頭文件#include<string>中。

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

   線性時間內執行———其複雜度爲O(n)

               即replace的執行要遍歷由區間[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;
}

結果如下:

用法二: 用str替換 迭代器起始位置 和 結束位置 的字符 
              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; 
}

結果如下:

用法三: 用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";
	string substr = "12345";
	str=str.replace(0,5,substr,substr.find("1"),4); //用substr的指定字符串替換str指定字符串
	cout << str << endl;
	return 0; 
}

結果如下:

用法四:string轉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; 
}

結果如下:

用法五:string轉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; 
}

結果如下:

用法六:string轉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; 
}

結果如下:

用法七:string轉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; 
}

結果如下:

用法八: 用重複n次的c字符替換從指定位置pos長度爲len的內容 

                  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);   //用重複3次的str1字符替換的替換從位置0~6的字符串
	cout<<str<<endl;
	return 0; 
}

結果如下:

用法九: 用重複n次的c字符替換從指定迭代器位置(從i1開始到結束)的內容 

            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);   //用重複3次的str1字符替換的替換從指定迭代器位置的內容 
	cout<<str<<endl;
	return 0; 
}

結果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章