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 that it is <string>, not <string. h>,. h is the header file using std: string; using std: wstring; or using namespace std in the C language. You can use string/wstring below, they correspond to char and wchar_t respectively. The usage of string is the same as that of wstring. Here we only use the string constructor: string (const char * s ); // use c string s to initialize string (int n, char c); // use n characters to initialize c. In addition, the string class also supports default constructor and copy constructor, for example, string s1; string s2 = "hello"; are all correct statements. If the constructed string is too long to be expressed, the length_error exception will be thrown. Character operations of the string class: const char & operator [] (int n) const; const char & at (int n) const; char & operator [] (int n); char & at (int n); operator [] and () returns the position of the nth character in the current string. However, the at function provides a range check. When the range is exceeded, an out_of_range exception is thrown. 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 copied strings. feature description: int capacity () const; // return the current capacity (that is, the number of elements that can be stored without adding memory to the string) int max_size () const; // return the maximum string Length int size () in the string object () const; // returns the int length () const of the current string; // returns the bool empty () const of the current string; // whether the current string is empty void resize (int len, char C); // set the current size of the string to len, and fill in the input and output operations of the string class with characters c: string heavy-duty operator> for input, similarly, operator <is 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 String connection: string & operator + = (const string & s); // connects 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 cstring & append (const_iterator first, const_iend Tor last); // compare the part of the iterator first and last with the string at the end of the current string: bool operator = (const string & s1, const string & s2) const; // compare two strings for equal operators ">", "<", "> =", "<= ","! = "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, <-1, = is returned, and 0 string substring is returned: string substr (int pos = 0, int n = npos) const; // return the string exchange consisting of n characters starting with the pos: void swap (string & s2); // exchange the search function of the string class of the current string and the s2 value: 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 the pos to find the position of string s in the current string // return the position when the query is successful, and return the string if the query fails :: npos value int rfind (char c, int pos = npos) const; // search for the position of character c in the current string from the pos start from the back to the 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; // 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; // open from pos Start to find the position where character c appears for the first time 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 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 (c Onst 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 checked forward Find the replacement function of the string class: string & replace (int p0, int n0, const char * s); // Delete the n0 characters starting with p0, then insert the string sstring & replace (int p0, int n0, const char * s, int n) at p0; // Delete the 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 p Start with 0 and then insert n characters cstring & replace (iterator first0, iterator last0, const char * s) at p0. // set [first0, last0) replace the string 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 fields in the string sstring & replace (iterator first0, iterator last0, int n, char c); // replace the fields in [first0, last0) with n characters. Cstring & replace (iterator first0, iterator last0, const_iterator first, const_iterator last); // replace the parts between [first0, last0) with [first, last) string: 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 Insert n characters citerator insert (iterator it, char c) at p0; // insert character c at it, return the position of the iterator after insertion void insert (iterator it, const_iterator first, const_iterator last); // insert the void insert (iterator it, int n, char c) character between [first, last) in it ); // insert n characters in it to the delete function iterator erase (iterator first, iterator last) of the c string class; // Delete All characters between [first, last, return the location of the deleted iterator erase (iterator it); // Delete the characters it points to, and return the location of the deleted iterator string & erase (int pos = 0, int n = n Pos); // Delete the n characters starting with the pos, and return the iterator processing of the modified string class: 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 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 and forward. By setting the iterator string: reverse_iterator, string :: const_reverse_iterator: Implements string stream processing by defining ostringstream and istringstream variables. # include <sstream> header files such as 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 ();

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.