自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++ replace()函數(shù)基本應(yīng)用方法總結(jié)

開發(fā) 后端
C++ replace()函數(shù)的各種應(yīng)用方式將會在這篇文章中為大家詳細介紹,希望朋友們可以從這里介紹的內(nèi)容充分掌握這一應(yīng)用技巧。

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

  1. basic_string::max_size  

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

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

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

與find 不同的是:rfind 默認從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<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 的字符

  1. basic _ string& replace( iterator _First0 , iterator _Last0 ,   
  2. const value _ type* _Ptr , size _ type _Num2 );   
  3. template<class InputIterator> 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 ( 下標)開始的 _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)容就為大家介紹到這里。

【編輯推薦】

  1. C++ CreateThread參數(shù)具體應(yīng)用技巧解析
  2. C++托管程序?qū)崿F(xiàn)安全管理
  3. C++文件拷貝應(yīng)用技巧探討
  4. C++ kmp算法模板代碼解讀
  5. C++ assert()函數(shù)應(yīng)用方式剖析
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-02-02 17:33:17

C++友元函數(shù)

2010-02-04 17:16:33

C++調(diào)用python

2010-02-02 14:45:35

C++ typeof

2010-02-06 16:16:01

C++冒泡排序

2010-03-01 11:24:31

WCF面向服務(wù)

2013-12-12 16:10:21

Lua腳本語言

2010-03-03 14:40:37

Python打包方法

2010-02-05 10:23:09

C++基本函數(shù)

2010-07-08 15:24:17

SNMP trap

2010-02-01 15:26:44

C++ inline函

2010-02-02 10:07:59

C++全局函數(shù)

2019-03-10 21:13:03

UbuntuLinuxAlfred

2010-01-25 13:19:44

C++詞法分析

2010-01-27 15:54:49

C++實現(xiàn)程序

2010-02-02 09:32:32

C++ typedef

2010-02-03 14:36:47

C++仿函數(shù)

2010-02-04 10:52:36

C++字符串分割函數(shù)

2010-03-03 16:40:55

Python HTTP

2010-02-25 10:52:29

WCF響應(yīng)服務(wù)

2010-02-26 13:40:28

WCF消息頭
點贊
收藏

51CTO技術(shù)棧公眾號