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

Source: Internet
Author: User

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

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 to initialize string (int n, char c); // use n characters for c Initialization

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 &amp;operator[](int n)const;
const char &amp;at(int n)const;
char &amp;operator[](int n);
char &amp;at(int n);
//Both operator [] and at() return the position of the nth character in the current string, but the at function provides range checking. When it is out of range, an out of range exception will be thrown. The subscript operator [] does not provide check access.
Const
Char * data() const; / / returns a non null terminated array of C characters
Const char * c_str() const; / / returns a null terminated C string
Int copy (char * s, int n, int POS = 0) const; / / copies the n characters in the current string starting with POS to the character array starting with s, and returns the actual number of copies

Feature description of string:

Int capacity() const; / / returns the current capacity (that is, the number of elements in string that can be stored without increasing memory)
Int max_size() const; / / returns the length of the largest 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 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 string class:
The overloaded operator > > of string class is used for input, and the overloaded operator < < is used for output.
The getline (istream &amp; in, string &amp; S); function is used to read strings from the input stream in to s, separated by the newline '\ n'.

String Value assignment:

Copy codeThe Code is as follows: 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:

Copy codeThe Code is as follows: string & operator + = (const string & s); // connects string s to the end of the current string
String & append (const char * s); // connect string s of the c type 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); // connects 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
String & append (const_iterator first, const_iterator last); // link the part of the iterator first and last to the end of the current string

String comparison:

Copy codeThe Code is as follows: bool operator = (const string & s1, const string & s2) const; // compare whether the 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:
Copy codeThe Code is as follows: string substr (int pos = 0, int n = npos) const; // returns a string consisting of n characters starting with pos.

String exchange:
Copy codeThe Code is as follows: 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 the string s in the current string
Int find (const string &amp; S, int POS = 0) const; / / start from POS to find the position of string s in the current string
//When the search is successful, it returns the location. If it fails, it returns the value of string:: NPOs
Int rfind (char c, int POS = NPOs) const; / / start from POS and find the position of character C in the current string 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 &amp;s,int pos = npos) const;
//Starting from POS, find the position of the string composed of the first n characters in the string s in the current string from the back to the front, and return the position successfully, and return the value of string:: NPOs in case of failure
Int find_first_of (char c, int POS = 0) const; / / find 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 &amp;s,int pos = 0) const;
//Start from POS to find the position of the first character in the current string in the array composed of the first n characters of S. String:: NPOs returned after lookup failure
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 &amp;s,int pos = 0) const;
//Find the location of the first character not in string s from the current string, and return string:: NPOs in failure
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 &amp;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 &amp;s,int pos = npos) const;
//Find ﹐ last ﹐ and find ﹐ last ﹐ not ﹐ are similar to find ﹐ first ﹐ and find ﹐ first ﹐ not ﹐ but only from the back to the front

Replacement functions of the string class:

String &amp; replace (int P0, int N0, const char * s); / / delete N0 characters from P0, and then insert string s at P0
String &amp; replace (int P0, int N0, const char * s, int n); / / delete the N0 characters at the beginning of P0, and then insert the first n characters of string s at P0
String &amp; replace (int P0, int N0, const string &amp; S); / / delete N0 characters from P0, and then insert string s at P0
String &amp; replace (int P0, int N0, const string &amp; S, int POS, int n); / / delete N0 characters from P0, and then insert n characters from POS in string s at P0
String &amp; replace (int P0, int N0, int n, char c); / / delete the N0 characters at the beginning of P0, and then insert n characters C at P0
String &amp; replace (iterator first0, iterator last0, const char * s); / / replace the part between [first0, last0) with the string s
String &amp; replace (iterator first0, iterator last0, const char * s, int n); / / replace the part between [first0, last0) with the first n characters of S
String &amp; replace (iterator first0, iterator last0, const string &amp; S); / / replace the part between [first0, last0) with string s
String &amp; replace (iterator first0, iterator last0, int n, char c); / / replace the part between [first0, last0) with n characters C
String &amp; replace (iterator first0, iterator last0, const \ iterator first, const \ iterator last); / / replace the part between [first0, last0) with the string between [first, last

Insert functions of the string class:

string &amp;insert(int p0, const char *s);
string &amp;insert(int p0, const char *s, int n);
string &amp;insert(int p0,const string &amp;s);
string &amp;insert(int p0,const string &amp;s, int pos, int n);
//The first four functions insert the first n characters of POS in string s at position P0
String &amp; insert (int P0, int n, char c); / / this function inserts n characters C at P0
Iterator insert (iterator it, char c); / / insert character c at it to return the position of iterator after insertion
Void insert (iterator it, const \ iterator first, const \ iterator last); / / insert characters between [first, last] at it
Void insert (iterator it, int n, char c); / / insert n characters C at it

Delete functions of the string class
Copy codeThe Code is as follows: 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 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 (); // returns the starting position of the string.
Const_iterator end () const;
Iterator end (); // returns the position after the last character of string
Const_iterator rbegin () const;
Iterator rbegin (); // returns the position of the last character of string
Const_iterator rend () const;
Iterator rend (); // returns the first character of the string.
Rbegin and rend are used for iterative access from the back and forward. They are 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!


Related Article

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.