溫馨提示×

溫馨提示×

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

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

C++中replace()函數(shù)的作用是什么

發(fā)布時間:2021-07-06 17:29:33 來源:億速云 閱讀:311 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關C++中replace()函數(shù)的作用是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

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

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 個字符

  1. basic _ string& replace( size _ type _Pos1 ,
    size _ type _Num1 , const value _ type* _Ptr );   

  2. basic _ string& replace(size _ type _Pos1 ,
    size _ type _Num1 ,const basic _ string _Str );   

  3. string a,b;   

  4. string s ( "AAAAAAAA" );   

  5. string s1p ( "BBB" );   

  6. const char* cs1p = "CCC" ;   

  7. a = s.replace ( 1 , 3 , s1p ); // s= ” ABBBAAAA ”   

  8. b = s.replace ( 5 , 3 , cs1p ); // s= ” ABBBACCC ”  

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

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

  1. basic _ string& replace( size _ type _Pos1 , 
    size _ type _Num1 , const basic _ string& _Str ,   

  2. size _ type _Pos2 , size _ type );   

  3. basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,   

  4. const value _ type* _Ptr , size _ type _Num2 );   

  5. string a, b;   

  6. string s ( "AAAAAAAA" );   

  7. string s2p ( "BBB" );   

  8. const char* cs2p = "CCC";   

  9. a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s= ” ABBAAAA ”   

  10. 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 的字符

  1. basic _ string&replace(iterator First0 ,iterator Last0 , 
    const basic _ string& _Str );   

  2. basic _ string&replace(iterator First0 ,iterator _Last0 , 
    const value _ type* _Ptr );   

  3. string s ( "AAAAAAAA" ); string s4p ( "BBB" );   

  4. const char* cs4p = "CCC";   

  5. basic_string<char>::iterator IterF0, IterL0;   

  6. IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;   

  7. string a, b;   

  8. a = s.replace ( IterF0 , IterL0 , s4p ); // s= ” BBBAAAAA ”   

  9. 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

  1. basic _ string substr( size _ type _Off = 0, 
    size _ type _Count = npos ) const;   

  2. string s("I love you!") , sub;   

  3. ssub=s.substr( ); // sub= ” I love you! ”   

  4. ssub=s.substr(1); // sub= ” love you! ”   

  5. ssub=s.substr(3,4); // sub= ” ove ”  

看完上述內容,你們對C++中replace()函數(shù)的作用是什么有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI