Summary of the usage of the string class in VC ++ Standard C ++, vcstring

Source: Internet
Author: User

Summary of the usage of the string class in VC ++ Standard C ++, vcstring

I believe that my friends who have used MFC programming should be very impressed with the CString class? Indeed, the CString class in MFC is very convenient and easy to use. But if you leave the MFC framework, is there any class that is very convenient to use? The answer is yes. Some people may say that, even if the MFC framework is not used, you can try to use the API in the MFC. The specific operation method is provided at the end of this article. In fact, many people may ignore the use of the string class in the Standard C ++. The string class function provided in standard C ++ is also very powerful and can be used in development projects. The specific usage section is listed as follows. It only plays a role in attracting others. Well, let's get started with the topic!

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

# Include <string> // Note: <string>, not <string. h>. h is the header file in 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 is the same as that of wstring. Here we only use string for introduction:

Constructor of the string class:

String (const char * s); & nbsp; // use c string s for initialization <BR> string (int n, char c); & nbsp; & nbsp; // initialize with n characters c

In addition, the string class also supports default constructor and copy constructor, such as string s1; string s2 = "hello. If the constructed string is too long to be expressed, the length_error exception is thrown;

String character operations:

Const char & operator [] (int n) const; const char & at (int n) const; char & operator [] (int n); char & at (int n ); // operator [] and at () both return the position of the nth character in the current string. However, the at function provides a range check and throws an out_of_range exception when the value is out of bounds, the subscript operator [] does not provide access check. Const char * data () const; // returns a non-null terminated c character array const char * c_str () const; // return 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

Feature description of string:

Int capacity () const; // returns the current capacity (that is, the number of elements in the string that can be stored without adding memory) int max_size () const; // returns the maximum string length int size () const that can be stored in the string object; // returns the current string size int length () const; // return 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, fill in the insufficient part with character c

Input and Output operations of the string class:
String type heavy-duty operator> used for input, also heavy-duty operator <used for output operations.
The getline (istream & in, string & s) function is used to read strings from the input stream in to s and separate them with line breaks '\ n.

String Value assignment:

String & operator = (const string & s); // assign string s to the current string & assign (const char * s ); // assign a value to string & assign (const char * s, int n) using string s ); // assign the value of string & assign (const string & s) to the n characters starting with string s; // assign string s to the current string & assign (int n, char c); // assign a value of n characters to the current string & assign (const string & s, int start, int n ); // assign the n characters starting from start in string s to the current string & assign (const_iterator first, const_itertor last ); // assign the part between the first and last iterator to the string

String connection:

String & operator ++ = (const string & s); // connect string s to the end string of the current string & append (const char * s ); // connect string s of the c type to string & append (const char * s, int n) at the end of the current string ); // connect the first n characters of the c-type string s to the string & append (const string & s) at the end of the current string; // same as operator ++ = () string & append (const string & s, int pos, int n ); // connect the n characters starting from pos in string s to the end string of the current string & append (int n, char c ); // Add n characters to the end of the current string cstring & append (const_iterator first, const_iterator last); // link the part between the first and last of the iterator 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 reloaded for string comparison; int compare (const string & s) const; // compare the size of the current string and 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 with the size of s int compare (int pos, int n, const string & s, int pos2, int n2) const; // compare the size of the string consisting of n characters starting from pos and n2 characters starting from // pos2 in s. 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; when the compare function is>, 1 is returned. If the value is <,-1 is returned. If the value is =, 0 is returned.

Substring of string:

String substr (int pos = 0, int n = npos) const; // returns a string consisting of n characters starting with pos.

String exchange:

Void swap (string & s2); // exchange the value of the current string and s2

String-type lookup functions:

Int find (char c, int pos = 0) const; // start from pos to find the position of character c in the current string int find (const char * s, int pos = 0) const; // start from the pos to find the position of string s in the current string int find (const char * s, int pos, int n) const; // start from pos to find the position of the First n characters in string s in the current string int find (const string & s, int pos = 0) const; // start from pos to find the position of string s in the current string // return the position when the query is successful. If the query fails, return the string: npos value int rfind (char c, int pos = npos) const; // search for the position of character c in the current string from the back to the back of the pos (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; // start from the pos and start from the back to the front to find the position of the string consisting of the First n characters in string s in the current string. The position of the string is returned successfully. If the position fails, the string is returned :: npos value int find_first_of (char c, int pos = 0) const; // search for the position where character c appears for the first time 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) co Nst; // start from pos to find the position of the first character in the array consisting of the First n characters of s in the current string. 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 string s from the current string, and return 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 and find_last_not_of are similar to find_first_of and find_first_not_of, but they are searched forward from the back.

Replacement functions of the string class:

String & replace (int p0, int n0, const char * s); // Delete the n0 characters starting with p0, and insert the string sstring & replace (int p0, int n0, const char * s, int n); // Delete n0 characters starting with p0, then insert the first n characters of string s in p0, string & replace (int p0, int n0, const string & s); // Delete the n0 characters starting from p0, then insert the string sstring & replace (int p0, int n0, const string & s, int pos, int n) at p0; // Delete the n0 characters starting with p0, then insert the string s at p0, which starts from pos, n Characters string & replace (int p0, int n0, int n, char c ); // Delete the n0 characters starting with p0, and then insert n characters cstring & replace (iterator first0, iterator last0, const char * s) at p0; // set [first0, replace the portion between last0 and sstring & replace (iterator first0, iterator last0, const char * s, int n); // replace [first0, last0) replace the previous n characters of string & replace (iterator first0, iterator last0, const string & s); // replace [first0, last0) replace the string sstring & replace (iterator first0, iterator last0, int n, char c); // replace [first0, last0) replace the fields with n characters cstring & replace (iterator first0, iterator last0, const_iterator first, const_iterator last); // replace [first0, last0) replace the portion of the string with the string between [first, last ).

Insert functions of the 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 four functions insert the first n characters of string & insert (int p0, int n, char c) Starting with pos in string s at the p0 position ); // This function inserts n characters citerator insert (iterator it, char c) at p0; // inserts character c at it, return void insert (iterator it, const_iterator first, const_iterator last); // insert the character void insert (iterator it, int n, char c); // insert n characters in it c

Delete functions of the string class

Iterator erase (iterator first, iterator last); // Delete All characters between [first, last), return the location of the deleted iterator erase (iterator it ); // Delete the characters pointed to by it, and return the position string & erase (int pos = 0, int n = npos) of the iterator after deletion; // Delete n characters starting with pos, returns the modified string.

String type iterator processing:
The string class provides the iterator for Traversing forward and backward. The iterator provides the syntax for accessing each character, similar to pointer operations. The iterator does not check the range.
Use string: iterator or string: const_iterator to declare the iterator variable. const_iterator cannot change the content of the iteration. Common iterator functions include:

Const_iterator begin () const; iterator begin (); // return the starting position of string const_iterator end () const; iterator end (); // returns the position const_iterator rbegin () const; iterator rbegin (); // returns the position const_iterator rend () const of the last character of string; iterator rend (); // returns the first rbegin and rend of the string to be used for iterative access from the back to the front. It is implemented by setting the iterator string: reverse_iterator, string: const_reverse_iterator

String stream processing:
Implemented by defining ostringstream and istringstream variables, # include <sstream> 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 it is used well, its functions will not be inferior to the CString class in MFC. Haha, my personal opinion!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.