string container
Article directory
container:
string
head File:
#include<string>
string is essentially a class, which is encapsulated inside the class char*
, and all strings are char*
containers of a type.
string constructor
constructor prototype
-
string();
//Create an empty string such as: string str (no parameter constructor)string(const char* s);
//Initialize with string s (constructor with parameters) -
string(const string& str);
//Use a string object to initialize another string object (copy constructor) -
string(int n,char c);
//Initialize with n characters c (constructor with parameters)
string assignment operation
The function prototype of the assignment:
-
string& operator = (const char* s)
; //char * type string is assigned to the current string -
string& operator = (const string &s);
//Assign the string s to the current string -
string& operator = (char c);
//Character assignment to the current string -
string& assign(const char *s);
//Assign the string s to the current string -
string& assign(const char *s, int n);
//Assign the first n characters of the string s to the current string -
string& assign(const string &s);
//Assign the string s to the current string -
string& assign(int n, char c);
// Assign n characters c to the current string
string string splicing
function prototype
-
string& operator+=(const char* str);
// Overload += operator -
string& operator+=(const char c);
// Overload += operator -
string& operator+=(const string& str);
// Overload += operator -
string& append(const char* s);
//Connect the string s to the end of the current string -
string& append(const char* str, int n);
//同operator+=(const string& str) -
string& append(const string& s);
//同operator+=(const string& str) -
string& append(const string& s, int pos, int n);
//The n characters starting from pos in the string s are connected to the end of the string
string find and replace
function prototype
int find(const string& str, int pos = 0) const;
//Find the first occurrence of str, starting from posint find(const char* s, int pos = 0) const;
//Find the first occurrence of s, starting from posint find(const char* s, int pos, int n) const;
//Find the first position of the first n characters of s from pos positionint find(const char c, int pos = 0) const;
//Find the first occurrence of character cint rfind(const string& str, int pos = npos) const;
//Find the last position of str, starting from posint rfind(const char* s, int pos = posn) const;
//Find the last occurrence of s, starting from posint rfind(const char* s, int pos, int n) const;
//Find the last position of the first n characters of s from posint rfind(const char c,int pos = 0) const;
//Find the last occurrence of character cstring& replace(int pos, int n, const string& str);
//Replace n characters starting from pos with the string strstring& replace(int pos, int n, const char* s);
//Replace the n characters starting from pos with the string s
string string comparison
- String comparisons are ASCII by character
=
return 0
>
return 1
<
return -1
function prototype
int compare(const string &s) const;
// compare with string sint compare(const char *s) const;
// compare with string s
string character access
There are two ways to access a single character in string
char& operator[](int n);
//Get characters by []char& at(int n);
//Get characters by at method
string insertion and deletion
function prototype
string& insert(int pos, const char* s);
//insert stringstring& insert(int pos, const string& str);
//insert stringstring& insert(int pos, int n, char c);
//Insert n characters c at the specified positionstring& erase(int pos, int n = npos);
//Delete n characters starting from pos
string substring
Functional description
- Get the desired substring from a string
function prototype
string substr(int pos = 0, int n = npos) const;
//Returns a string consisting of n characters starting from pos