Summary of the usage of the string class in Standard C ++

Source: Internet
Author: User

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); // use the C string s for initialization
String (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. 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 current string is out of bounds, the subscript operator [] does not provide access check.
Const char * Data () const; // returns a non-null ending 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

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 length of a string that can be stored in a string object.
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 null
Void resize (INT Len, char C); // set the current size of the string to Len and fill the missing 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
String & assign (const char * s); // use the C-type string s to assign a value
String & assign (const char * s, int N); // assign values with n characters starting with string s
String & assign (const string & S); // assign string s to the current string
String & assign (int n, char C); // assign a value to the current string with n characters C
String & assign (const string & S, int start, int N); // assign the n characters starting from start in 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 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 starting from POs in string s to the end of the current string
string & append (int n, char C ); // Add n characters to the end of the current string. C
string & append (const_iterator first, const_iterator last ); // connect the part of 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 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 S and the string consisting of n characters starting from POS of the current string
Int compare (INT POs, int N, const string & S, int pos2, int N2) const; // compares the string consisting of n characters starting from POS with S.

// The size of a string consisting of N2 characters starting with 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;
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 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; // search for the position of string s in the current string from POS
// Return the location when the search is successful. If the search 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 POs to the forward
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;
// 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. If the string fails, return the value of string: NPOs.
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) const;
// 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 the string s from the current string. If the string fails to appear, string: NPOs is returned.
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 s at P0
String & replace (INT P0, int N0, const char * s, int N); // Delete N0 characters starting with P0, insert the first n characters of string s in P0.
String & replace (INT P0, int N0, const string & S); // Delete the N0 characters starting with P0, and insert the string s at P0
String & replace (INT P0, int N0, const string & S, int POs, int N); // Delete N0 characters starting with P0, then insert n characters starting from POs in string s at P0
String & replace (INT P0, int N0, int N, char C); // Delete N0 characters starting with P0, and then insert n characters C at P0
String & replace (iterator first0, iterator last0, const char * s); // Replace the parts between [first0, last0) with the string s
String & replace (iterator first0, iterator last0, const char * s, int N); // Replace the portion between [first0, last0) with the first n characters of S
String & replace (iterator first0, iterator last0, const string & S); // Replace the parts between [first0, last0) with the string s
String & replace (iterator first0, iterator last0, int N, char C); // Replace the parts between [first0, last0) with N characters C
String & replace (iterator first0, iterator last0, const_iterator first, const_iterator last); // Replace the parts between [first0, last0) with strings 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 starting with pos in the string s at the P0 position
String & insert (INT P0, int N, char C); // This function inserts n characters in P0 C
Iterator insert (iterator it, char C); // insert character C in it to return the position of the iterator after insertion
Void insert (iterator it, const_iterator first, const_iterator last); // insert characters between [first, last) in it
Void insert (iterator it, int N, char C); // insert n characters in it

Delete functions of the string class
Iterator erase (iterator first, iterator last); // deletes all characters between [first, last) and returns the position of the iterator after deletion.
Iterator erase (iterator it); // deletes the characters it points to and returns the position of the iterator after deletion.
String & erase (INT Pos = 0, int n = NPOs); // Delete the n characters starting with POS and return the modified string

string iterator processing:
the string class provides the iterator for Traversing forward and backward, the iterator provides the syntax for accessing various characters, 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 start position of the string
const_iterator end () const;
iterator end (); // returns the position after the last string character
const_iterator rbegin () const;
iterator rbegin (); // return the position of the last string character
const_iterator rend () const;
iterator rend (); // return the first character of the string.
rbegin and rend are used for iterative access from the back and forward. You can set the iterator string: reverse_iterator, string :: const_reverse_iterator implementation

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 = "", 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!

Finally, we will introduce how to applyProgramReference the partial classification in MFC, such as cstring.

1. Right-click the project directory and choose "properties" ---> "configuration properties" ---> "general" ---> "use of MFC" ---> "use MFC in a static library ",

The default value is "use standard Windows libraries", for example:

2. include # include <afxwin. h>, for example, it can be found in stdafx. the front of the H file contains # include <afxwin. h> header file.Source codeYou can use

Cstring class, but this also has a drawback, that the compiled program is much larger than the original one. I tried a small program and chose "use standard Windows libraries" to compile it.

The release version of is about 92kb, And the release version compiled by "use MFC in a static library" is about 192kb, Which is kb in size. This is my personal consideration ......

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.