Detailed explanation of STL in C + +

1. string class header file: string

If s has three characters, s[3] of traditional C string is' 0 ', but C + + string is only s[2].

(1) common constructors

string(): construct an empty string class object, which is an empty string
 string(const char* s): use C language string to construct string class object
 string(size_t n,char c): the string class object after construction includes n characters C
 String (const string & S): construct another object with the copy of string class object s
 String (const string & S, size_t n): use the nth character in object s to start constructing a new string class object

(2) common functions

size_t size() const: Returns the valid character length of a string
size_t length() const: Returns the valid length of a string
size_t capacity() const: Returns the current capacity (i.e string Number of elements that can be stored without increasing memory)
bool empty() const: Judge whether the string is empty, yes true,Not return false
void clear():Clear valid characters
void reserve(size_t res_arg = 0): modify string Class to reserve space for Strings
int max_size()const;   Return string The length of the maximum string that can be stored in an object
void resize(int len,char c);Set the current string size to len,More, less, more characters c Under filled parts

string &insert(int p,const string &s);  //Insert string s in position p
string &replace(int p, int n,const char *s); //Delete n characters from P and insert string s at p
string &erase(int p, int n);  //Delete n characters from the beginning of p and return the modified string
string substr(int pos = 0,int n = npos) const;  //Returns a string of n characters from pos
void swap(string &s2);    //Exchange the value of current string and s2
string &append(const char *s);   //Connect string s to the end of the current string
void push_back(char c)   //Add a character c to the end of the current string
const char *data()const;   //Returns a non null terminated array of C characters. data(): similar to c_str(), it is used to convert string to const char * where the array it returns is not terminated with null characters,
const char *c_str()const;  //Returns a null terminated C string, that is, the C Φ str() function returns a pointer to a regular C string, the content of which is the same as the string string string, and is used for string to const char*
int find(char c,size_t pos=0) const;//Find the character c backward from the pos position of the string, find the position of the character returned in the string, and return - 1 not found
int rfind(char c, size_t pos = npos);//Looks up the character c from the pos position of the string and returns the position of the character in the string

size_type find( const basic_string &str, size_type index );  //Returns the location of the first occurrence of str in the string (starting from index). If not, returns string::npos
size_type find( const char *str, size_type index );  // Ditto
size_type find( const char *str, size_type index, size_type length );  //Returns the first occurrence of str in the string (starting from index, with length). If it is not found, returns string::npos
size_type find( char ch, size_type index );  // Returns the first occurrence of the character ch in the string (starting from index). If it is not found, returns string::npos

Note: to find whether string a contains substring b, instead of stra. Find (STRB) > 0, use stra. Find (STRB)! = string: npos. (npos can represent the end bit of a string. It is of type string::type_size, which is the type returned by find(). The find function returns string::npos if the specified value cannot be found.)

(4)

Tags: C++ less C

Posted on Sun, 03 Nov 2019 13:22:12 -0800 by wkoneal