C replace 函式用法詳解

2021-07-22 15:29:55 字數 2299 閱讀 3716

本文主要針對c++中常用replace函式用法給出樣例程式

[cpp]view plain

copy

/*用法一:

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

*string& replace (size_t pos, size_t len, const string& str);

*/int

main()    

執行結果:

[cpp]view plain

copy

/*用法二:

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

*string& replace (const_iterator i1, const_iterator i2, const string& str);

*/int

main()    

執行結果:

[cpp]view plain

copy

/*用法三:

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

*string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);

*/int

main()    

執行結果:

[cpp]view plain

copy

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

*用str替換從指定位置0開始長度為5的字串

*string& replace(size_t pos, size_t len, const char* s);

*/int

main()    

執行結果:

[cpp]view plain

copy

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

*用str替換從指定迭代器位置的字串

*string& replace (const_iterator i1, const_iterator i2, const char* s);

*/int

main()    

執行結果:

[cpp]view plain

copy

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

*用s的前n個字元替換從開始位置pos長度為len的字串

*string& replace(size_t pos, size_t len, const char* s, size_t n);

*/int

main()    

執行結果:

[cpp]view plain

copy

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

*用s的前n個字元替換指定迭代器位置(從i1到i2)的字串

*string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);

*/int

main()    

執行結果:

[cpp]view plain

copy

/*用法八:

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

*string& replace (size_t pos, size_t len, size_t n, char c);

*/int

main()    

執行結果:

[cpp]view plain

copy

/*用法九:

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

*string& replace (const_iterator i1, const_iterator i2, size_t n, char c);

*/int

main()    

執行結果:

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

sscanf函式用法詳解

sscanf函式用法詳解 名稱 sscanf 從乙個字串中讀進與指定格式相符的資料.函式原型 int sscanf string str,string fmt,mixed var1,mixed var2 int scanf const char format argument 說明 sscanf與s...

linux select函式用法詳解

函式主要功能 呼叫select 函式,可以讓程序指示核心等待多個事件中的任意乙個發生,並使程序處於阻塞狀態,直到 select 引數指定的事情或者關心的事情發生時候,才喚醒程序,並且函式返回準備就緒的事件個數。函式原型 include int select int maxfdp1 fd set re...

sscanf函式用法詳解

sscanf 從乙個字串中讀進與指定格式相符的資料.函式原型 int sscanf string str,string fmt,mixed var1,mixed var2 int scanf const char format argument 說明 sscanf與scanf類似,都是用於輸入的,只...