STL-string (typical operation demo)

Source: Internet
Author: User

STL-string (typical operation demo)
1String Concept
String is a string type of STL, which is usually used to represent strings. Before using string, the string is usually represented by char. String and char * can both be used to represent strings. What is the difference between the two.
Comparison between string and char *
String is a class, and char * is a pointer to a character.
String encapsulates char * and manages this string. It is a char * container.
String does not need to consider memory release or out-of-bounds.
String manages the memory allocated by char. For each string copy, the value is maintained by the string class. Do not worry about copying out-of-bounds or out-of-range values.
String provides a series of string operation functions (this will be detailed later)
Find, copy, delete erase, replace, and insert the constructor of insert2string
Default constructor:
String (); // construct an empty string s1.
Copy constructor:
String (const string & str); // construct a string that is the same as str. For example, string s1 (s2 ).
Constructors with Parameters
String (const char * s); // use string s for initialization
String (int n, char c); // use n characters c to initialize the 3string access character operation
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 nth character in the current string, but the two are different.
The main difference is that at () throws an exception when it crosses the border. [] returns (char) 0 when it just crosses the border. When it continues to cross the border, the compiler directly fails. If your program wants to catch exceptions through try and catch, we recommend that you use (). 4. Get const char * from string
Const char * c_str () const; // returns the first address of a string ending with ''. 5. Copy the string to the memory space pointed to by char *.
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. Make sure that the space pointed to by s is large enough to accommodate the current string, otherwise it will cross the border. Length of 6 string
Int length () const; // returns the length of the current string. The length does not include ''at the end of the string ''.
Bool empty () const; // whether the current string is null 7string value assignment
String & operator = (const string & s); // assign string s to the current string
String & assign (const char * s); // assign string s to the current string
String & assign (const char * s, int n); // assign the first n characters of string s to the current string
String & assign (const string & s); // assign string s to the current string
String & assign (int n, char c); // assign n characters c to the current string
String & assign (const string & s, int start, int n); // assign the n characters starting from start in string s to the 8string connection of the current string.
String & operator ++ = (const string & s); // connects string s to the end of the current string
String & operator ++ = (const char * s); // connects string s to the end of the current string
String & append (const char * s); // connects string s to the end of the current string
String & append (const char * s, int n); // connects the first n characters of 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); // compare the number of c9string characters added to the end of the current string
Int compare (const string & s) const; // compare with string s
Int compare (const char * s) const; // comparison with string s
When the compare function is>, 1 is returned, and <-1, = is returned. The comparison is case-sensitive. For comparison, refer to the dictionary order, and the smaller the front is. Uppercase A is smaller than lowercase. 10string substring
String substr (int pos = 0, int n = npos) const; // returns a substring consisting of n characters starting with pos.

Search and replace 11string
Search
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 string & s, int pos = 0) const; // search for the position of string s in the current string from pos
If the find function cannot be found,-1 is returned.
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 string & s, int pos = npos) const;
// Rfind indicates reverse lookup. If no result is found,-1 is returned.

Replace
String & replace (int pos, int n, const char * s); // Delete n characters starting from pos, and insert string s at pos
String & replace (int pos, int n, const string & s); // Delete the n characters starting from the pos, and insert the string s at the pos.
Void swap (string & s2); // exchange the value of the current string and s2

Delete and insert 12String intervals
String & insert (int pos, const char * s );
String & insert (int pos, const string & s );
// The first two functions Insert the string s at the pos position
String & insert (int pos, int n, char c); // insert n characters in the pos Position c

String & erase (int pos = 0, int n = npos); // Delete the n characters starting with pos and return the modified string

Demo

# Include
 
  
# Include
  
   
# Include
   
    
# Include using namespace std; // string initialization void initString () {cout <init of string object; string s1 = lucifer; string s2 (zhang); string s3 = s2; // The object s3string s4 (10, 'A'); cout <s1: <s1 <endl; cout <s2: <s2 <endl; cout <s3: <s3 <endl; cout <s4: <s4 <endl; cout <endl ;} // traverse stringvoid ergodic () {cout <ergodic of string object; string s = lucifer; // 1 array mode for (unsigned in T I = 0; I <s. length (); I ++) {cout <s [I] <'';} cout <endl; // 2 iterator mode for (string :: iterator it = s. begin (); it! = S. end (); it ++) {cout <* it <'';} cout <endl; // 3at () method try {for (unsigned int I = 0; I <s. length () + 3; I ++) {cout <s. at (I) <''; // throw an exception} cout <endl;} catch (...) {cout <exception occurred;} cout <endl; // If subscript is used, the program will directly stop/* try {for (unsigned int I = 0; I <s. length () + 3; I ++) {cout <s [I] <'';} cout <endl;} catch (...) {} * // conversion of character pointers and strings void char2string () {string s = lucifer; // string-> char * co Ut <s. c_str () <endl; // string content copy. In buf, char buf [128] = {0}; s. copy (buf, 3, 0); // note that only copy3 characters are required and will not become a C-style string cout <buf: <buf <endl; cout <endl ;}// string connection void stringAppend () {string s1 = lucifer; string s2 = zhang; s1 = s1 + s2; cout <s1: <s1 <endl; string s3 = 333; string s4 = 444; s3.append (s4); cout <s3: <s3 <endl; cout <endl ;} // string search and replace void stringFindAndReplace () {string S = lucifer hello lucifer 111 lucifer 222 lucifer 333; int index = s. find (lucifer, 0); // search for cout starting from position subscript 0 <index: <index <endl; // find the number of times lucifer appears, the array subscript int offIndex = s. find (lucifer, 0); while (offIndex! = String: npos) {cout <off index: <offIndex <endl; offIndex = s. find (lucifer, ++ offIndex);} // replace lucifer in s with offoffoffindex = s. find (lucifer, 0); while (offIndex! = String: npos) {cout <off index: <offIndex <endl; s. replace (offIndex, 7, LUCIFER); offIndex = s. find (lucifer, ++ offIndex);} cout <after replace: <s <endl; cout <endl; // after replace: LUCIFER hello LUCIFER 111 LUCIFER 222 LUCIFER 333} // cut (delete range) and insert void stringDelete () {string s = hello lucifer hello; string: iterator it = find (s. begin (), s. end (), 'L'); if (it! = S. end () {s. erase (it) ;}cout <after delete 'l': <s <endl; // after delete 'l': helo lucifer hellos. erase (s. begin (), s. end (); // delete all cout <after delete all: <s <endl; // after delete all: string s2 = zhang; s2.insert (0, lucifer); // cout <after insert: <s2 <endl; // after insert: luciferzhangs2.insert (s2.length (), hello ); // cout <after insert: <s2 <endl; // after insert: luciferzhanghellocout <endl;} void stringTransform () {string s = LuciferZhang; transform (s. begin (), s. end (), s. begin (), toupper); cout <s: <s <endl; // s: LUCIFERZHANGtransform (s. begin (), s. end (), s. begin (), tolower); cout <s: <s <endl; // s: luciferzhangcout <endl;} int main () {initString (); ergodic (); char2string (); stringAppend (); stringFindAndReplace (); stringDelete (); stringTransform (); return 0 ;}
   
  
 


 

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.