C++中replace()函數(shù)使用方法匯總
C++編程語言中的string應(yīng)用方式多樣化,每一種應(yīng)用方式都能幫助我們提實(shí)現(xiàn)特定的功能需求。在這里我們將會為大家詳細(xì)介紹一下其中一個(gè)比較重要的用法,有關(guān)C++ replace()函數(shù)的應(yīng)用方式。
basic_string::max_size
C++ replace()函數(shù)返回string 能放的最大元素個(gè)數(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。返回找到的第一個(gè)string 下標(biāo)值;如果沒找到則返回npos。
與find 不同的是:rfind 默認(rèn)從npos 開始找。其他相同。
basic_string::replace
將原string 中的元素或子串替換。返回替換后的string。
(1)用string 或C-string 代替操作string 中從 _Pos1 開始的 _Num1 個(gè)字符
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 個(gè)字符,代替操作string 中從 _Pos1 開始的 _Num1 個(gè)字符
用C-string 中的 _Num2 個(gè)字符,代替操作string 中從 _Pos1 開始的 _Num1 個(gè)字符
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 個(gè)character _Ch , 代替操作string 中從 _Pos1 開始的 _Num1 個(gè)字符
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 個(gè)字符,代替操作string 中從 First0 到 Last0 的字符
用C-string 中的 _Num2 個(gè)字符,代替操作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 個(gè)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
交換兩個(gè)string。
void swap( basic _ string& _Str ); s1.swap ( s2 ); basic_string::substr
返回從 _Off ( 下標(biāo))開始的 _Count 個(gè)字符組成的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)容就為大家介紹到這里,希望對大家學(xué)習(xí)C++中replace()函數(shù)使用方法有所幫助。
相關(guān)文章
C語言中結(jié)構(gòu)體、聯(lián)合體的成員內(nèi)存對齊情況
這篇文章主要給大家介紹了關(guān)于C語言中結(jié)構(gòu)體、聯(lián)合體的成員內(nèi)存對齊情況的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
學(xué)習(xí)C和C++的9點(diǎn)經(jīng)驗(yàn)總結(jié)
本文給大家總結(jié)了一下我們在學(xué)習(xí)C和C++的時(shí)候的一些經(jīng)驗(yàn)和需要注意的事項(xiàng),希望能給大家一些幫助,少走些彎路2015-12-12
Visual Studio2000系列版本安裝OpenGL的圖文教程
這篇文章主要介紹了Visual Studio2000系列版本安裝OpenGL的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
利用C語言實(shí)現(xiàn)任務(wù)調(diào)度的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用純C語言實(shí)現(xiàn)任務(wù)調(diào)度(可用于STM32、C51等單片機(jī)),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04
C++普通函數(shù)指針與成員函數(shù)指針實(shí)例解析
這篇文章主要介紹了C++普通函數(shù)指針與成員函數(shù)指針,很重要的知識點(diǎn),需要的朋友可以參考下2014-08-08
C語言數(shù)據(jù)結(jié)構(gòu)中堆排序的分析總結(jié)
堆是計(jì)算機(jī)科學(xué)中一類特殊的數(shù)據(jù)結(jié)構(gòu)的統(tǒng)稱,通常是一個(gè)可以被看做一棵完全二叉樹的數(shù)組對象。而堆排序是利用堆這種數(shù)據(jù)結(jié)構(gòu)所設(shè)計(jì)的一種排序算法。本文將通過圖片詳細(xì)介紹堆排序,需要的可以參考一下2022-04-04

