(C / C ++ learning) string of container 24.STL

Description: C style strings (ends with \ 0) too complicated, C ++ library defines a string class header file <string>.

* String and c-style string comparison:

1. char * is a pointer, string is a class, string encapsulates char *,

2. string char * automatically manage the allocated memory, do not worry about the occurrence of various cross-border acts like c string like that.

* String operations common container

1. Constructor

. 1  String STR; // Create an empty string 
2  String str1 (STR); // create a new object using an existing object 
. 3  String ( const  char * s); // string s initialization 
. 4  String str2 ( int n- , char c); // use of n initialization character c

2. The basic assignment

. 1  String & operator = ( const  char * s); // char * string assigned to this type of string 
2  String & operator = ( const  String & s); // the current string is assigned the string s 
. 3  String & operator = ( char C); // character assigned to the current string 
. 4  string & aSSIGN ( const  char * s); // the current string is assigned the string s 
. 5  string & aSSIGN ( const  char * s, int n-); //The first n characters of the string s is assigned to the current string 
. 6  String & ASSIGN ( const  String & s); // string assigned to the current string 
. 7  String & ASSIGN ( int n, char C); // with c n characters assigned to the current string 
. 8  string & aSSIGN ( const  string & s, int start, int n); // will start from the beginning of the string s n characters assigned to

3. Character access operation

. 1  char & operator [] ( int n-); // taken by the character [] embodiment 
2  char & at ( int n-); // Get a character by a method at

4. string splicing operation

. 1  String & operator + = ( const  String & STR); // Overload operator + = 
2  String & operator + = ( const  char * STR); // Overload operator + = 
. 3  String & operator + = ( const  char C); // overloaded + = operator 
. 4  string & the append ( const  char * s); // string is connected to the current end of the string s 
. 5  string & the append ( const  char * s, int n-); / /The first n characters of the string s is connected to the current end of the string 
. 6  String & the append ( const  String & s); // with operator + = () 
. 7  String & the append ( const  String & s, int POS, int n);
 . 8  / / string s starting from the n characters pos connected to the current end of the string 
. 9  string & the append ( int n, char c); // add n c at the end of the current character string

5. Find and Replace

. 1  int Find ( const  String & str, int POS = 0 ) const ; // find the first occurrence str position, start searching at the POS 
2  int Find ( const  char * s, int POS = 0 ) const ; // Find s first time position, from the beginning to find pos 
. 3  int Find ( const  char * s, int pos, int n) const ; // Find position pos s from the first n characters of the first position 
. 4  int Find ( const  char C ,int POS = 0 ) const ; // find the location of the first occurrence of the character c 
5  int rfind ( const  String & str, int POS = npos) const ; // Find the last position str, start looking from POS 
6  int rfind ( const  char * s, int pos = NPoS) const ; // Find the last occurrence of s position, from the beginning to find pos 
. 7  int rfind ( const  char * s, int pos, int n) const ; // Find pos s from the first n the last character position 
8 int rfind ( const  char c, int pos = 0 ) const ; // Find the last position of the character c appears 
. 9  String & Replace ( int pos, int n, const  String & STR); // replace n characters from the beginning of pos string STR 
10  string & replace ( int pos, int n, const  char * s); // replace pos n characters from the beginning of the string s

6. Comparison of Operation

1  / * Compare function returns> 1, -1 <, the return == 0, case-insensitive comparison, when compared to a reference sequence of the dictionary, the smaller the preceding row, uppercase A is smaller than the lowercase A * / 
2  int Compare ( const  string & s) const ; // comparison string s 
. 3  int Compare ( const  char * s) const ; // comparison string s

7. substring

. 1  String substr ( int pos = 0 , int n = NPoS) const ; // Returns n characters from the beginning of the string consisting of pos

8. The insert and delete operations

. 1  String & INSERT ( int POS, const  char * S); // insertion string S 
2  String & INSERT ( int POS, const  String & STR); // insertion string STR 
. 3  String & INSERT ( int POS, int n- , char c); // insert characters c n in the specified location 
. 4  String & ERASE ( int pos, int n = NPoS); // delete n characters from the start pos

And c style string conversion 9.string

1 //string转char*
2 string str = "itcast";
3 const char* cstr = str.c_str();
4 //char*转string 
5 char* s = "itcast";
6 string str(s);

Note: in the presence of a * c ++ string to the implicit type conversion, automatic type conversions do not exist from a string from the object to the style c const char. For string type string () function returns the string corresponding to the object by C_string c_str. Typically, the programmer should stick string class object in the entire process, until the content must be converted to char * when converted into c_string .

Guess you like

Origin www.cnblogs.com/tuihou/p/12404497.html