The replace () function in C + + uses the method to summarize the _c language

Source: Internet
Author: User
Tags first string

String applications in the C + + programming language are diverse, and each application helps us to implement specific functional requirements. Here we will give you a detailed introduction to one of the more important uses of the C + + replace () function.

Basic_string::max_size 

The C + + replace () function returns the maximum number of elements that a 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 

Look for the given string. Returns the first string subscript found, or returns NPOs if it is not found.

Unlike find is: RFind default from NPOs start looking. The other is the same.

Basic_string::replace 

Replaces an element or substring in the original string. Returns the replaced string.

(1) using string or c-string instead of the _NUM1 characters starting from _pos1 in the operation 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) _num2 characters starting from _pos2 in the C + + replace () function instead of the _NUM1 characters starting from _pos1 in the operation string

_num2 characters in c-string instead of _NUM1 characters starting from _pos1 in the operation 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) Use _count character _ch instead of _num1 characters starting from _pos1 in the operation string

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) Use string or c-string to replace characters from First0 to Last0 in the operation 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) _num2 characters starting from _pos2 in the C + + replace () function instead of the characters from First0 to Last0 in the operation string

Replaces the characters from First0 to Last0 in the operation string with the _num2 characters 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) using _count character _ch instead of the characters from First0 to Last0 in the operation string

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

void Swap (Basic _ string& _str);  
S1.swap (S2);  
Basic_string::substr 

Returns a string of _count characters starting from _off (subscript)

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 () function of the relevant content for you to introduce here, I hope you learn C + + in the Replace () function to use the method to help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.