The replace of function in C++ USES a method summary

  • 2020-05-05 11:39:48
  • OfStack

The C++ programming language USES string in a variety of ways, each of which helps us to achieve specific functional requirements. Here we will introduce one of the more important USES of the C++ replace() function in detail.


basic_string::max_size 

The C++ replace() function returns the maximum number of elements that string can put. (different from 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 

Find the given string. Returns the first string value found; If not, return npos.

Unlike find, rfind starts from npos by default. Everything else is the same.


basic_string::replace 

Replace the element or substring in the original string. Returns the string after the substitution.

(1) replaces the _Num1 character

from _Pos1 in the string operation string or C-string

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) replaces the _Num1 character

in the C++ replace() function with _Pos2 from _Pos2

The _Num2 character in C-string replaces the _Num1 character

from _Pos1 in string

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) replaces the _Num1 character

in operation string with _Count character _Ch

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) replaces the characters

from First0 to Last0 in string by string or C-string

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) replaces

from First0 to Last0 with _Num2 characters starting from _Pos2 in the C++ replace() function

Replace

from First0 to Last0 in string with the _Num2 character in C-string

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) replaces the characters

in operation string from First0 to Last0 with _Count character _Ch

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

Swap two string.


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

Returns string

with the characters of _Count starting with _Off (subscript)

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

C++ replace() function related content for you to introduce here, hope to help you learn replace() function C++ replace() function.


Related articles: