C++ STL replace()函數常用用法詳解

來源:互聯網
上載者:User

標籤:

replace演算法:

                replace函數包含於標頭檔#include<string>中。

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

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

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

 

 

下面介紹replace()函數常用的九中方法:

 

用法一:

            用str替換指定字串從起始位置pos開始長度為len的字元

            

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";line=line.replace(line.find("&"),1,"1");//將line中的第一個&替換成1 cout<<line<<endl;return 0;

 

 

用法二:

           用str替換 迭代器起始位置 到 結束位置 的字元 

 

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";line=line.replace(line.begin(),line.begin()+6,"1");//將line從begin位置開始的6個字元替換成1 cout<<line<<endl;return 0;}


 

用法三:

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

 

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";string substr="012345";line=line.replace(0,5,substr,substr.find("1"),3);//將line字串0到5位置上的字元替換為substr的指定子串(從'1'位置開始的3個字元) cout<<line<<endl;return 0;}


 

 

用法四:

           用str替換大串指定位置上的子串(string轉char*時編譯器可能會報出警告,不建議這樣做 )

 

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";char* str="012345";line=line.replace(0,5,str);//用str替換從指定位置0開始長度為5的字串 cout<<line<<endl;return 0;}

 

 

用法五:

           用str替換從指定迭代器位置的字串 (string轉char*時編譯器可能會報出警告,不建議這樣做 )

 

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";char* str="012345";line=line.replace(line.begin(),line.begin()+9,str);cout<<line<<endl;return 0;}


 

 

用法六:

          用s的前n個字元替換從開始位置pos長度為len的字串 (string轉char*時編譯器可能會報出警告,不建議這樣做 )

 

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";char* str="012345";line=line.replace(0,9,str,5);//用str的前5個字元替換從0位置開始長度為9的字串 cout<<line<<endl;return 0;}

 

用法七:

           用s的前n個字元替換指定迭代器位置(從i1到i2)的字串 (string轉char*時編譯器可能會報出警告,不建議這樣做 )

 

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";char* str="012345";line=line.replace(line.begin(),line.begin()+9,str,5);//用str的前5個字元替換指定迭代器位置的字串  cout<<line<<endl;return 0;}


 

 

用法八:

           用重複n次的c字元替換從指定位置pos長度為len的內容 

 

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";char c='1';line=line.replace(0,9,3,c);//用重複3次的c字元替換從指定位置0長度為9的內容 cout<<line<<endl;return 0;}


 

 

用法九:

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

 

範例程式碼:

#include<iostream>#include<string>using namespace std;int main(){string line="qwer& &qwer&& &&";char c='1';line=line.replace(line.begin(),line.begin()+9,3,c);//用重複3次的c字元替換從指定迭代器中的內容 cout<<line<<endl;return 0;}


 

 

註:

      有使用迭代器類型的參數不限於string類型,可以為vector、list等其他類型迭代器。

 

 

 

 

 

 

          

 

 

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

C++ STL replace()函數常用用法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.