Summary of the usage of the string class in standard C++ (reproduced)

Original link

I believe that friends who have used MFC programming should be very impressed with this class of CString, right? Indeed, the CString class in MFC is really convenient and easy to use. But if you leave the MFC framework, is there any class that is very convenient to use like this? The answer is yes. Some people may say that even if you don't use the MFC framework, you can find a way to use the API in MFC. The specific operation method is given at the end of this article. In fact, many people are likely to ignore the use of the string class in standard C++. The string class provided in standard C++ is also very powerful, and can generally meet our needs when developing projects. Part of the specific usage is listed as follows, and it will only serve as an introduction. Okay, let's not talk nonsense, and go directly to the topic!

To use the string class in standard C++, you must include

#include // Note that it is not <string.h>, the header file with .h is the C language

using std::string;

using std::wstring;

or

using namespace std;

Now you can use string/wstring, which correspond to char and wchar_t respectively.

The usage of string and wstring is the same, the following only uses string as an introduction:

The constructor of the string class:

string(const char *s); //Initialize
string with c string s (int n,char c); //Initialize with n characters c.
In addition, the string class also supports default constructor and copy constructor, such as string s1 ;String s2="hello"; is the correct way of writing. When the constructed string is too long to express, it will throw a length_error exception;

Character operation of string type:
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n); both
operator[] and at() Returns the position of the nth character in the current string, but the at function provides range checking, and out_of_range exception will be thrown when it crosses the range. The subscript operator [] does not provide check access.
const char *data()const;//returns a non-null-terminated c character array
const char *c_str()const;//returns a null-terminated c string
int copy(char *s, int n, int pos = 0) const;//Copy the n characters starting with pos in the current string to the character array starting with s, and return the actual number of copies

Characteristic description of string:
int capacity()const; //Returns the current capacity (that is, the number of elements that can be stored in the string without increasing the memory)
int max_size()const; //Returns the largest string that can be stored in the string object Length
int size()const; //returns the size of the current string
int length()const; //returns the length of the current string
bool empty()const; //whether the current string is empty
void resize(int len,char c);//Set the current size of the string to len, and fill the insufficient part with the character c

Input and output operations of the
string class : The overloaded operator operator>> of the string class is used for input, and the overloaded operator operator<< is used for output operations.
The function getline(istream &in,string &s); is used to read a string from the input stream in to s, separated by a newline character'\n'.

Assignment of
string : string &operator=(const string &s);//Assign string s to the current string
string &assign(const char *s);//Assign
string with c type string s &assign(const char *s, int n);//Assign n characters starting with c string s
string &assign(const string &s);//Assign string s to current string
string &assign(int n,char c);//Use n Assign the characters c to the current string
string &assign(const string &s,int start,int n);//Assign the n characters from start in the string s to the current string
string &assign(const_iterator first,const_itertor last) ;//Assign the part between the first and last iterators to the string

String connection:
string &operator+=(const string &s);//Connect the string s to the end of the current string
string &append(const char *s); //Connect the c type string s to the end of the current string
string &append(const char *s,int n);//Connect the first n characters of the c type string s to the end of the current string
string &append(const string &s); //same as operator+=()
string &append(const string &s,int pos,int n);//Connect the n characters from pos in the string s to the end of the current string
string &append(int n,char c); //Add n at the end of the current string The character c
string &append(const_iterator first, const_iterator last);//Connect the part between the iterator first and last to the end of the current string

String comparison:
bool operator==(const string &s1,const string &s2)const;//Compare whether two strings are equal
operator">","<",">=","<=","! =" Both are overloaded for string comparison;
int compare(const string &s) const;//Compare the current string and the size of s
int compare(int pos, int n,const string &s)const;//Compare The size of the current string consisting of n characters starting from pos and s
int compare(int pos, int n,const string &s,int pos2,int n2)const;//Compare the current string starting from pos to n Character string with s

//The size of the string consisting of n2 characters at the beginning of pos2
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const; The
compare function returns 1 when >, -1 when <, and 0 when ==

Substring of string:
string substr(int pos = 0,int n = npos) const;//returns a string of n characters starting at pos

Exchange of string:
void swap(string &s2); //Exchange the value of the current string and s2

The search function of the string class:
int find(char c, int pos = 0) const;//Find the position of character c in the current string starting from pos
int find(const char *s, int pos = 0) const;// Find the position of the string s in the current string from pos
int find(const char *s, int pos, int n) const;//Find the position of the first n characters in the string s in the current string from pos
int find(const string &s, int pos = 0) const;//Find the position of string s in the current string starting from pos
//return the position when the search succeeds, return the value of string::npos on failure
int rfind(char c , int pos = npos) const;//Find the position of character c in the current string starting from pos from back to front
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = NPoS) const;
int rfind (const string & s, int pos = NPoS) const;
// Find the string from the string s pos first n characters from the string forward in the current Position, return the position on success, return the value of string::npos on failure
int find_first_of(char c, int pos = 0) const;//Find the position of the first occurrence of character c from pos
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//start from pos Find the position of the first character in the array consisting of the first n characters of s in the current string. Finding failure returns string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const ;
int find_first_not_of(const string &s,int pos = 0) const;
//Find the position of the first character not in the string s from the current string, and return on failure string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

The replacement function of the string class:
string &replace(int p0, int n0,const char *s);//Delete n0 characters starting from p0, and then insert the string s
string &replace(int p0, int n0,const char *s, int n);//Delete n0 characters starting from p0, then insert the first n characters of
string s at p0 string &replace(int p0, int n0,const string &s);//Delete starting from p0 N0 characters from p0, then insert the string s
string &replace(int p0, int n0,const string &s, int pos, int n);//delete the n0 characters from p0, and then insert the string s at p0 N characters
string starting from pos &replace(int p0, int n0,int n, char c);//delete n0 characters starting from p0, and then insert n characters at p0 c
string &replace(iterator first0, iterator last0 ,const char *s);//Replace the part between [first0, last0) with the string s
string &replace(iterator first0, iterator last0, const char *s, int n);//put [first0, last0) Replace the part between the first n characters of s
string &replace(iterator first0, iterator last0, const string &s);//Replace the part between [first0, last0) with string s
string &replace(iterator first0, iterator last0,int n, char c);//Replace the part between [first0, last0) with n characters c
string &replace(iterator first0, iterator last0, const_iterator first, const_iterator last); //Replace the part between [first0, last0) with the string between [first, last)

Insert function of string class:
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0, const string &s, int pos, int n);
//The first 4 functions insert the first n characters starting at pos in the string s at position p0
string &insert(int p0, int n, char c);//This function is in Insert n characters at p0 c
iterator insert(iterator it, char c);//Insert character c at it and return the position of the
iterator after insertion void insert(iterator it, const_iterator first, const_iterator last);//in it Insert the characters between [first, last) at
void insert(iterator it, int n, char c);//Insert n characters at it c

The deletion function of the string class
iterator erase(iterator first, iterator last);//delete all characters between [first, last), and return the position of the
iterator after deletion iterator erase(iterator it);//delete the character pointed to by it , Return the position of the iterator after deletion
string &erase(int pos = 0, int n = npos);//Delete n characters from the beginning of pos and return the modified string

Iterator processing of the
string class : The string class provides an iterator for forward and backward traversal. The iterator provides a syntax for accessing individual characters, similar to pointer operations, and the iterator does not check the range.
Use string::iterator or string::const_iterator to declare iterator variables. const_iterator is not allowed to change the content of the iteration. Common iterator functions are:
const_iterator begin()const;
iterator begin(); //Returns the starting position of the string
const_iterator end()const;
iterator end(); //Returns the position after the last character of the string
const_iterator rbegin( )const;
iterator rbegin(); //Returns the position of the last character of the string
const_iterator rend()const;
iterator rend(); //Returns the front of the first character position of the string
rbegin and rend are used from back to front Iterative access is achieved by setting the iterator string::reverse_iterator, string::const_reverse_iterator

String stream processing:
by defining ostringstream and isringstream variables, in the #include header file
For example:
string input("hello,this is a test");
istringstream is(input);
string s1,s2,s3,s4;
is> >s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
ostringstream os;
os<<s1<<s2< <s3<<s4;
cout<<os.str();

The above is a brief introduction to the C++ string class. If you use it well, its functions will not be much inferior to the CString class in MFC, hehe, personal opinion!

Finally, we will introduce how to reference some classes in MFC, such as CString, in Win32 applications.

1. Right-click in the project directory and select "Properties"—>"Configuration Properties"—>"General"—>"Use of MFC"—>"Use MFC in a Static Library",

The default is: "Use Standard Windows Libraries", as shown below: Insert picture description here
2. Include #include <afxwin.h> before all the header files you use, for example: you can include #include <afxwin.h at the top of the stdafx.h file. h> header file, so you can use it in your source code

CString class, but this also has a disadvantage, that is, the compiled program is much larger than the original one. I tried a small program, select "Use Standard Windows Libraries" to compile it

The Release version is about 92kb, and the Release version compiled with "Use MFC in a Static Library" is about 192kb, which is a full 100kb. This is a personal consideration...
Just for your own reference, please go to the original author’s article to read the article. , The boss has a lot of great blogs

Guess you like

Origin blog.csdn.net/ZXG20000/article/details/104131711