1.assign ()
Prototype:
String (1) basic_string& Assign (const basic_string& str);//substring (2) basic_string& Assign (const basic _string& str, size_type subpos, Size_type sublen);//c-string (3) basic_string& assign (const chart* s);//buffer (4 ) basic_string& Assign (const chart* s, Size_type N);//ill (5) basic_string& Assign (size_type N, CharT c);//range ( 6) Template <class inputiterator>
Ps:chart is the 1th parameter of the class template basic_string that specifies the type of character in the string. Instantiate basic_string with Char, and get a string (refer to "What exactly is C + + string" below). So, in string, the chart is char.
Example:
#include <iostream> #include <string>using namespace Std;int main () {string str1 ("123456"); String str = "123456789"; char* str2 = "abcdef"; Str.assign (STR1); cout << str << endl;//123456 Visible Execution Assign (), STR will be rewritten str.assign (STR1, 2, 3); cout << str << endl;//345 3 characters starting from position 2 str.assign (str1, 1, 20);//out of range cout << str << endl;/ /23456 cout << str.length () << endl;//5 visible No spaces str.assign (STR1, 2, Str1.npos);//Introduction NPOs cout << ; Str << endl;//3456 from position 2 to the end of the character to Str str.assign (5, ' X '); cout << str << endl;//xxxxx str.assign (Str1.begin (), Str1.end ()-1); cout << str << endl;//12345 from the beginning to the end-1 (that is, the last character)//str.assign (Str1.begin () + 4, Str1.end ()-4);//cannot be reversed, that is to try to Tr1.begin () + 4 (5) begins with 5432 to Str1.end ()-4 (2) to STR is not working//cout << str << Endl; This will cause run-time error str.assign (Str1.begin () + 2, Str1.end ()-3),//pointing to the same character (3), which can cout << str << ENDL;//3 str.assign (Str1.begin () + 2, Str1.end ()-4);//This can be cout << str << ; endl;//Empty line cout << str.length () << endl;//0 str.assign ("ABCDEFG", 6); cout << str << endl;//abcdef The first 6 characters of ABCDEFG to str str.assign ("ABCDEFG", 20);//out of range cout << str <& Lt endl;//abcdefg+ garbled cout << str.length () << ENDL;//10 description will "abcdefg+3 a space" to Str str.assign (3, 0x41); cout << str << ENDL;//AAA can use 16 binary ASCII code 0X41 to 10 binary is 65, is a ASCII code str.assign (STR2); cout << str << endl;//abcdef str.assign (str2, 2, 3); cout << str << ENDL;//CDE can return 0 for char* type;}
There is a sentence in the above code
Str.assign (STR1, 2, Str1.npos);
Here we explain NPOs:
It is defined as follows:
Std::string::npos
static const size_t NPOs =-1;
It is the maximum value of the size_t type, and some compilers also set it to the maximum capacity of a string object (in bytes, including '% '), while some compilers are not, as we will see in the rear.
Why is it the biggest?
-1 is stored in the back code (32 bits), is 32 1, and size_t is unsigned integer, so 32 1 of 2 is converted to 10 binary is 2 32 square-1, or 4294967295.
Look at the following validation code:
#include <iostream> #include <string>using namespace Std;int main () { string str1 = "123456789"; cout << str1.npos << endl;//4294967295 cout << (str1.npos =-1) << endl;//1 cout << ; Str1.max_size () << endl;//4294967294 1073741820 (=2^30-4) return 0;}
Of these, 4294967294 is the result on the VS2013, Visible max_size () does not include ' s ', 1073741820 is the result on the CodeBlocks12.11.
Thus, we know that Str.assign (str1, 2, Str1.npos) is equivalent to Str.assign (STR1, 2, 4294967294);
Also, in c++11, there are two overloaded assign () functions whose prototypes are:
Initializer list (7) basic_string& assign (initializer_list<chart> il);//move (8) basic_string& Assign ( basic_string&& str) noexcept;
Where (7) is related to the list container, and (8) is related to rvalue reference and move semantics, which we do not dwell on here.
For the Noexcept keyword in (8), it declares that the function does not throw an exception (of course, the function does not really throw an exception, for example, the function a (declared as noexcept) calls another function B, and in function B there is an exception statement, so when we call function A, May throw an exception), because Noexcept is not the focus of this article, we do not detail here, interested readers can check their own information to learn about the relevant content.
2.swap ()
Prototype: void swap (basic_string& str);
Only this one, in exchange for the entire string, there is no operation for the substring.
no example.
3.erase ()
Prototype:
Sequence (1) basic_string& erase (Size_type pos = 0, Size_type len = NPOs);//character (2) iterator erase (Iterat or P);//range (3) iterator erase (iterator first, iterator last);
Description
Notice that the basic_string& erase (size_type pos = 0, Size_type len = NPOs), gives the default parameters so that when we use the function, we can omit the 2nd argument, The effect is to delete all characters from the position where the 1th parameter is located (including the position) (as in the following example, the operation and output of STR3). The reason is Len=npos, and NPOs must be larger than the maximum length of the string object.
Example:
#include <iostream> #include <string>using namespace Std;int main () {string str1 ("123456789"); String str2 ("123456789"); String Str3 ("123456789"); String Str4 ("123456789"); String STR5 ("123456789"); String STR6 ("123456789"); String Str7 ("123456789"); String Str8 ("123456789"); Str1.erase (2, 3); cout << str1 << endl;//126789 str2.erase (2, 10); cout << endl;//Empty line str3.erase (7);//Note that the parameter value here cannot exceed str3.length (), otherwise a run-time error will be equal to the output blank line, which is equivalent to deleting ' \ ' and the following (of course there is nothing, Because ' it ' is already the last one) cout << str3 << endl;//1234567 When you omit the 2nd argument, delete all characters from the position where the 1th argument is located (that is, the position)//str4.era SE (10, 5);//Do not do//cout << STR4 << Endl; Str4.erase (Str4.begin ()); cout << STR4 << endl;//23456789 str5.erase (Str5.begin () + 2); cout << str5<< endl;//12456789//str6.erase (str6.begin () + 10);//Do not do so//cout << str6 << End L Str6.erase (Str6.end ()-2); cout << str6 << endl;//12345679//str7.erase (Str7.end ()-10);//Do not//cout << str7 << Endl; Str7.erase (Str7.begin () +2,str7.end ()-2); Str7.erase (Str7.begin () + 2, Str7.end ()-2); cout << str7 << endl;//1289 Delete all characters from Str7.begin () +2 (including Str7.begin () +2) to Str7.end ()-2 (including Str7.end ()-2)//s Tr8.erase (Str8.begin () + 7, Str8.end ()-5);//Do not//cout << str8 << Endl; return 0;}
4.clear ()
Prototype:
void clear () noexcept;
Role:
Deletes all the contents of the string.
Example:
#include <iostream> #include <string>using namespace Std;int main () { string str ("123456789"); Str.clear (); cout << str << endl;//blank line cout << str.length () << endl;//0 return 0;}
5.insert ()
Prototype:
String (1) basic_string& Insert (size_type pos, const basic_string& str);//substring (2) basic_string& Insert (size_type pos, const basic_string& STR, size_type subpos, Size_type sublen);//c-string (3) basic_string& Amp Insert (size_type POS, const chart* s),//buffer (4) basic_string& Insert (size_type POS, const chart* S, Size_type N); /fill (5) basic_string& Insert (size_type pos, size_type N, CharT c); Iterator Insert (Const_iterator p, Size_type N, CharT c);//single character (6) iterator Insert (const_iterator p, cha RT c);//range (7) template <class inputiterator> iterator Insert (iterator p, Inputiterator first, Inputiterator last);//initializer list (8) basic_string& Insert (const_iterator p, initializer_list<chart> il );
Description
1) the prototype (8) is related to the list container and is a new standard for c++11, which is not detailed here.
2) The return values and parameters appearing in the above prototypes are iterator and inputiterator related to iterators, which are not detailed here, and can be simply understood as pointers, and the return values of Begin () and end () match them.
Example:
#include <iostream> #include <string>using namespace Std;int main () {/* * Test basic_string& Insert (size_ty PE POS, const basic_string& str); */String St1 ("123456789"); String St2 ("123456789"); String str1 ("abc"); St1.insert (3, str1); cout << st1 << endl;//123abc456789 Note the insertion of//st2.insert (ten, str1) in front of Pos point, or//out of range//cout << st2 &L t;< endl;//can not * * * * Test basic_string& Insert (size_type pos, const basic_string& str,size_type subpos, size _type Sublen); */String St2 ("123456789"); String St3 ("123456789"); String str2 ("ABCDEFG"); St2.insert (3, STR2, 2, 3); cout << st2 << endl;//123cde456789 st3.insert (3, STR2, 4, 9);//out of range cout << st3 << ENDL;//12 3efg456789 visible if the length of the string is exceeded, it continues until the end of the string, without filling the space/* * * * Test basic_string& Insert (size_type POS, const chart* s); */String St4 ("123456789"); String St5 ("123456789"); char* STR3 = "abc"; St4.insert (3, STR3); cout << st4 << endl;//123abc456789 st5.insert (3, "abc"); cout << st5 << endl;//123abc456789 * * * * Test basic_string& Insert (size_type POS, const chart* S, size_ Type N); */String St6 ("123456789"); String St7 ("123456789"); St6.insert (3, "ABCDEFG", 3);//n can be 0, 0 o'clock nothing adds a run-time error when n is negative cout << st6 << endl;//123abc456789 st7.inser T (3, "ABCDEFG", 20);//out of range cout << st7 << ENDL;//123ABCDEFG i n v a l 456789 adjust large n value (500) to test, found in the middle more out of the error Statements and garbled, and each run of the output may be different//codeblocks12.11 on the middle is directly garbled * * * Test basic_string& Insert (Size_type pos , Size_type N, CharT c); and iterator Insert (Const_iterator p, Size_type N, CharT c); */String St8 ("123456789"); String St9 ("123456789"); St8.insert (3, 2, ' a '); cout << st8 << endl;//123aa456789 St9.insert (St9.begin () + 3, 2, ' a '); cout << st9 << endl;//123aa456789 * * * * Test iterator Insert (Const_iterator p, CharT c); */String SS1 ("123456789"); Ss1.insert (Ss1.begin () +3, ' a ');//By prototype, you cannot change Ss1.begin () +3 to 3 cout << SS1 << endl;//123a456789 * * * Test T Emplate <class inputiterator> * * Iterator Insert (iterator p, Inputiterator first, Inputiterator last); */String SS2 ("123456789"); String Str4 ("ABCDEFG"); Ss2.insert (Ss2.begin () + 3, Str4.begin (), Str4.begin () +3)///out of range there is a run-time error cout << SS2 << endl;//123abc456789 return 0;}
6.append ()
Prototype:
String (1) basic_string& append (const basic_string& str);//substring (2) basic_string& Append (const basic _string& str, size_type subpos, Size_type sublen);//c-string (3) basic_string& append (const chart* s);//buffer (4 ) basic_string& Append (const chart* s, Size_type N);//fill (5) basic_string& append (size_type N, CharT c);//range (6) Template <class inputiterator>basic_string& append (inputiterator First, inputiterator last);// Initializer list (7) basic_string& append (initializer_list<chart> il);
(7) not detailed.
Example:
#include <iostream> #include <string>using namespace Std;int main () {/* * * Test basic_string& Append (const basic_string& str); */String St1 ("123456789"); String str1 ("abc"); string& ss = St1.append (STR1); cout << SS << endl;//123456789abc cout << st1 << ENDL;//123456789ABC * * * Test basic_string& Amp Append (const basic_string& str, size_type subpos, Size_type Sublen); */String St2 ("123456789"); String St3 ("123456789"); String str2 ("ABCDEFG"); St2.append (STR2, 2, 3); cout << st2 << endl;//123456789cde st3.append (str2, 3, 20);//out of range cout << st3 << endl;//1234 56789DEFG when a number represents a range, if it goes out of range, it does not fill a space until the end of the string, and there is no run-time error cout << st3.length () << ENDL;//13 Nonetheless, in actual programming, You must also ensure that you do not exceed the range/* * Test basic_string& Append (const chart* s); */String St4 ("123456789"); St4.append ("abc"); cout << st4 << endl;//123456789abc/* * * Test basic_string& Append (const chart* s, Size_type N); */String St5 ("123456789"); St5.append ("abc", 5);//out of range cout << st5 << endl;//123456789abc+ garbled * * * Test basic_string& append (si Ze_type N, CharT c); */String St6 ("123456789"); St6.append (3, 0x41);//16 ASCII code cout << ST6 << endl;//123456789aaa * * * Test template <class INP Utiterator> **basic_string& Append (inputiterator First, Inputiterator last); */* String st7 ("123456789"); String Str3 ("ABCDEFG"); St6.append (Str3.begin () + 2, Str3.begin () + 10);//out of range cout << st7 << endl;//when using iterators, a run-time error occurs when the iterator is out of range */ return 0;}
7.replace ()
Prototype:
String (1) basic_string& replace (size_type pos, Size_type len, const basic_string& str); basic_string& ; Replace (const_iterator I1, const_iterator i2, const basic_string& str),//substring (2) basic_string& replace ( Size_type Pos, Size_type len, const basic_string& STR, size_type subpos, Size_type sublen);//c-s Tring (3) basic_string& replace (size_type pos, Size_type len, const chart* s);basic_string& replace (const _iterator I1, const_iterator i2, const chart* s);//buffer (4) basic_string& replace (size_type pos, Size_type len, Const chart* S, size_type N);basic_string& replace (const_iterator i1, const_iterator i2, const chart* s, Size_typ e n);//fill (5) basic_string& replace (size_type pos, Size_type len, Size_type N, CharT c);basic_string& re Place (const_iterator I1, Const_iterator i2, Size_type N, CharT c);//range (6) template <class inputiterator> Basic_ string& Replace (const_iterator I1, Const_iterator I2, Inputiterator First, Inputiterator last);//initializer list (7) Basi c_string& Replace (const_iterator i1, const_iterator i2, initializer_list<chart> il);
(7) does not detail
Example: (Here is only a partial prototype test, other readers can refer to the example of the previous function for self-testing)
#include <iostream> #include <string>using namespace Std;int main () {/* * * Test basic_string& replace (size _type Pos, Size_type len, const basic_string& str); */String St1 ("123456789"); String str1 ("ABCDE"); String str2 ("AB"); St1.replace (2, 3, str1);//3 characters starting at position 2 (345) are replaced with ABCDE cout << st1 << endl;//12abcde6789 string St2 ("12345678 9 "); St2.replace (1, 7, str1); cout << st2 << endl;//1abcde9 string st3 ("123456789"); St3.replace (6, 9, str2);//out of range cout << st3 << endl;//123456ab * * * Test basic_string& replace (const _iterator I1, const_iterator i2, const basic_string& str); *//*string St4 ("123456789"); St4.replace (St4.begin () + 8, St4.begin () + ten, str1); Iterator out of range, with run-time error cout << st4 << endl;*//* * * Test basic_string& replace (size_type POS, Size_typ e len, const chart* S, Size_type N); */String St5 ("123456789"); St5.replace (2, 3, "ABCDEFG ", 5); cout << st5 << endl;//12abcde6789 string St6 ("123456789"); St6.replace (2, 3, "ABC", 20);//out of range cout << st6 << endl;//12abc+ garbled +6789 for char* type, if out of range, there will be garbled return 0;}
The meaning of the prototypes and parameters of the above functions is similar, the reader can comprehend by analogy. In combination with the above examples, the "out of scope" of this particular case gives a general summary of the law:
1. For a string type, the range is represented by a number, which, when exceeded, is automatically cut to the end of the string, does not fill a space, is not garbled, and does not have a run-time error.
2. For string types, a run-time error occurs when an iterator is used to represent a range that is exceeded.
3. For the char* type, the range is represented by a number (only as a numeric representation, char* is the basic type, no iterator), and garbled when exceeded.
A brief analysis of the function of string content modification and substitution in c++string