日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++replace()函數(shù)基本應(yīng)用方法總結(jié)

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

成都創(chuàng)新互聯(lián)長期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為蓮都企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、網(wǎng)站制作,蓮都網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

 
 
 
  1. basic_string::max_size 

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

 
 
 
  1. size _ type max _ size( ) const; 
  2. basic_string ::size_type cap, max; 
  3. cap = s.capacity ( ); 
  4. max = s.max_size ( ); // max=4294967294. 
  5. basic_string::rfind 

尋找給定的string。返回找到的***個string 下標(biāo)值;如果沒找到則返回npos。

與find 不同的是:rfind 默認(rèn)從npos 開始找。其他相同。

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

 
 
 
  1. basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , 
  2. size _ type _Count , value _ type _Ch ); 
  3. string result; 
  4. string s ( "AAAAAAAA" ); 
  5. char ch = 'C'; 
  6. 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::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 的字符

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

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

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

交換兩個string。

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

返回從 _Off ( 下標(biāo))開始的 _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ù)的相關(guān)內(nèi)容就為大家介紹到這里。


文章標(biāo)題:C++replace()函數(shù)基本應(yīng)用方法總結(jié)
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/djesdss.html