欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中replace()函數(shù)使用方法匯總

 更新時間:2022年04月27日 08:13:59   投稿:lijiao  
這篇文章主要介紹了C++中replace()函數(shù)使用方法匯總,在這篇文章中為大家詳細介紹C++ replace()函數(shù)的各種應(yīng)用方式,希望朋友們可以從這里介紹的內(nèi)容充分掌握這一應(yīng)用技巧

C++編程語言中的string應(yīng)用方式多樣化,每一種應(yīng)用方式都能幫助我們提實現(xiàn)特定的功能需求。在這里我們將會為大家詳細介紹一下其中一個比較重要的用法,有關(guān)C++ replace()函數(shù)的應(yīng)用方式。

basic_string::max_size 

C++ replace()函數(shù)返回string 能放的最大元素個數(shù)。(不同于capacity)

size _ type max _ size( ) const;  
basic_string <char>::size_type cap, max;  
cap = s.capacity ( );  
max = s.max_size ( ); // max=4294967294.  
basic_string::rfind 

尋找給定的string。返回找到的第一個string 下標值;如果沒找到則返回npos。

與find 不同的是:rfind 默認從npos 開始找。其他相同。

basic_string::replace 

將原string 中的元素或子串替換。返回替換后的string。

(1)用string 或C-string 代替操作string 中從 _Pos1 開始的 _Num1 個字符

basic _ string& replace( size _ type _Pos1 ,
size _ type _Num1 , const value _ type* _Ptr );  
basic _ string& replace(size _ type _Pos1 ,
size _ type _Num1 ,const basic _ string _Str );  
string a,b;  
string s ( "AAAAAAAA" );  
string s1p ( "BBB" );  
const char* cs1p = "CCC" ;  
a = s.replace ( 1 , 3 , s1p ); // s= ” ABBBAAAA ”  
b = s.replace ( 5 , 3 , cs1p ); // s= ” ABBBACCC ” 

(2)用C++ replace()函數(shù)中從 _Pos2 開始的 _Num2 個字符,代替操作string 中從 _Pos1 開始的 _Num1 個字符

用C-string 中的 _Num2 個字符,代替操作string 中從 _Pos1 開始的 _Num1 個字符

basic _ string& replace( size _ type _Pos1 , 
size _ type _Num1 , const basic _ string& _Str ,  
size _ type _Pos2 , size _ type );  
basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,  
const value _ type* _Ptr , size _ type _Num2 );  
string a, b;  
string s ( "AAAAAAAA" );  
string s2p ( "BBB" );  
const char* cs2p = "CCC";  
a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s= ” ABBAAAA ”  
b = s.replace ( 4 , 3 , cs2p , 1 ); // s= ” ABBAC ” 

(3)用 _Count 個character _Ch , 代替操作string 中從 _Pos1 開始的 _Num1 個字符

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,  
size _ type _Count , value _ type _Ch );  
string result;  
string s ( "AAAAAAAA" );  
char ch = 'C';  
result = s.replace ( 1 , 3 , 4 , ch ); // s= ” ACCCCAAAA ” 

(4)用string 或C-string ,代替操作string 中從 First0 到 Last0 的字符

basic _ string&replace(iterator First0 ,iterator Last0 , 
const basic _ string& _Str );  
basic _ string&replace(iterator First0 ,iterator _Last0 , 
const value _ type* _Ptr );  
string s ( "AAAAAAAA" ); string s4p ( "BBB" );  
const char* cs4p = "CCC";  
basic_string<char>::iterator IterF0, IterL0;  
IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;  
string a, b;  
a = s.replace ( IterF0 , IterL0 , s4p ); // s= ” BBBAAAAA ”  
b = s.replace ( IterF0 , IterL0 , cs4p ); // s= ” CCCAAAAA ” 

(5)用C++ replace()函數(shù)中從 _Pos2 開始的 _Num2 個字符,代替操作string 中從 First0 到 Last0 的字符

用C-string 中的 _Num2 個字符,代替操作string 中從 First0 到 Last0 的字符

basic _ string& replace( iterator _First0 , iterator _Last0 ,  
const value _ type* _Ptr , size _ type _Num2 );  
template<class InputIterator> basic _ string& replace(  
iterator _First0 , iterator _Last0 ,  
InputIterator _First , InputIterator _Last );  
IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;  
IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;  
a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );  
b = s.replace ( IterF1 , IterL1 , cs5p , 4 ); 

(6)用 _Count 個character _Ch , 代替操作string 中從 First0 到 Last0 的字符

basic _ string& replace( iterator _First0 , iterator _Last0 ,  
size _ type _Count , value _ type _Ch );  
a = s.replace ( IterF2 , IterL2 , 4 , ch );  
basic_string::swap 

交換兩個string。

void swap( basic _ string& _Str );  
s1.swap ( s2 );  
basic_string::substr 

返回從 _Off ( 下標)開始的 _Count 個字符組成的string

basic _ string substr( size _ type _Off = 0, 
size _ type _Count = npos ) const;  
string s("I love you!") , sub;  
ssub=s.substr( ); // sub= ” I love you! ”  
ssub=s.substr(1); // sub= ” love you! ”  
ssub=s.substr(3,4); // sub= ” ove ” 

C++ replace()函數(shù)的相關(guān)內(nèi)容就為大家介紹到這里,希望對大家學習C++中replace()函數(shù)使用方法有所幫助。

相關(guān)文章

  • C++標準庫bitset類型的簡單使用方法介紹

    C++標準庫bitset類型的簡單使用方法介紹

    這篇文章主要介紹了C++標準庫bitset類型的簡單使用方法,需要的朋友可以參考下
    2017-07-07
  • C++實現(xiàn)聊天小程序

    C++實現(xiàn)聊天小程序

    這篇文章主要為大家詳細介紹了C++實現(xiàn)聊天小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 解決c++?error:crosses?initialization?of?問題

    解決c++?error:crosses?initialization?of?問題

    最近在寫代碼的時候,碰到了?crosses?initialization?of?...?的問題,只因我在?switch?的某個?case?分支下定義了一個變量,于是乎便將這個問題整理一下,需要的朋友可以參考下
    2023-03-03
  • C++短路求值(邏輯與、邏輯或)實例

    C++短路求值(邏輯與、邏輯或)實例

    這篇文章主要介紹了C++短路求值(邏輯與、邏輯或)實例,以實例形式講述了邏輯或的短路與邏輯與的短路及相應(yīng)的應(yīng)用實例,需要的朋友可以參考下
    2014-10-10
  • 詳解C語言之文件操作(上)

    詳解C語言之文件操作(上)

    這篇文章主要介紹了關(guān)于C語言文件操作方法的相關(guān)資料,小編覺得這篇文章寫的還不錯,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-11-11
  • C語言實例梳理講解常用關(guān)鍵字的用法

    C語言實例梳理講解常用關(guān)鍵字的用法

    關(guān)鍵字是C語言非常重要的一部分,熟練的掌握和使用關(guān)鍵字有助于我們更加熟悉了解C語言,同時C語言的關(guān)鍵字也是面試筆試中??嫉膬?nèi)容。C語言的關(guān)鍵字共有32個,但并不是每個關(guān)鍵字都有坑,本篇文章將通過理論聯(lián)系實際的方式為大家講解C語言中易混易錯以及??嫉囊恍╆P(guān)鍵字
    2022-05-05
  • Qt實現(xiàn)給窗口繪制陰影的示例代碼

    Qt實現(xiàn)給窗口繪制陰影的示例代碼

    這篇文章主要為大家詳細介紹了Qt實現(xiàn)給窗口繪制陰影的方法,文中的示例代碼講解詳細,對我們學習Qt有一定的幫助,感興趣的可以了解一下
    2022-11-11
  • c語言10個經(jīng)典小程序

    c語言10個經(jīng)典小程序

    c語言的經(jīng)典程序,學習c語言的初學者可以參考下
    2013-01-01
  • 詳解C++ 動態(tài)內(nèi)存分配與命名空間

    詳解C++ 動態(tài)內(nèi)存分配與命名空間

    這篇文章主要介紹了詳解C++ 動態(tài)內(nèi)存分配與命名空間,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • 詳細分析C++ 異常處理

    詳細分析C++ 異常處理

    這篇文章主要介紹了C++ 異常處理的的相關(guān)資料,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06

最新評論