string operations summary

A, string initialization

void solve(){
    str = "Hello world" ;
    char cstr[] = "abcde";
    string s1 (str); // string copy string
    cout << s1 << endl;
    string s2 (str, 2); // s2 from str begin copying subscript 2
    cout << s2 << endl;
    string s3 (str, 2, 3) // s3 str subscript 2 from the start of copy length 3
    cout << s3 << endl;
    string s4 (cstr); // array of characters can be copied to the string
    cout << s4 << endl;
    string s5 (cstr, 3); // Note that string and copy a bit different: the ctr "front" three characters copies.
    cout << s5 << endl;
    string s6 (5, 'A'); // generates a string containing 5 'A' character
    cout << s6 << endl;
    string s7 (str.begin (), str.begin () + 5); // copy interval [0,5] characters in
    << endl << S7 COUT; 
  String S8 = str.substr (2) // str is the subscript 2 from the start to the end of the copy S8
   String str.substr S9 = (2,. 3) // str from the subscript 3 is a copy of length 2 starts to s9.
}

Two, string comparison operations, etc.

You can use ==,>, <,> =, <=, and! = String comparison, may be + or + = operator to connect two strings, and may be used [] acquired specific characters.

    str.compare ( "abcd") - exactly equal, returns 0
    str.compare ( "dcba") - returns a value less than 0
    str.compare ( "ab") - Returns the value greater than 0
    str.compare (str) - equal

 

Three, string characterization

 

void solve(){
    cout << str.capacity () << endl; // return the current capacity (i.e., without increasing the number of elements in the string can be stored in memory)
    // returns the maximum string length of string objects that can be stored; cout << str.max_size () << endl
    cout << str.size () << endl; // return the current size of the string
    cout << str.length () << endl; // return the string length of the current
    cout << str.empty () << endl; // if the current string is empty, empty, else returns of 0. The 
   str.resize (10) // set the size str is 10, and if the size is large the current length of the string, \ filled 0 str.resize (10, 'a') ;. // string size is set to the current len, and are 'A'
   str.reserve (10) disposed // str volume 10, is not populated
   str.clear (); // empty string }

 Four, string of commonly used functions

 

   str.assign ( "ABC"); // empty string, and is set to "ABC".
    str.swap (str1) // exchanged and str string str1
    / * Add operations * /
    str.push_back ( 'A'); // add a character 'A' at the end str, parameters must be in the form of characters
    str.append ( "ABC"); // add a character string "ABC" at the end str, string parameters must be
    / * Insert function * /
    str.insert (2, 3, 'A'); // add three characters 'A' to position 2 in the subscript str
    str.insert (2, "ABC"); // add the string "ABC" in the position 2 of the subscript str
    str.insert (str.begin (), str1.begin (), str1.end ()) // the str1 inserted at the beginning of str.
    / * Erase function * /
    "All deleted after" str.erase (2) // delete subscript 2 start position, including 2,
    str.erase (2, 1) // delete at position 2 marked the start of a deleted after
    str.erase (str.begin () + 2) // Delete character subscript 2
    /*find*/
    str.find ( 'A'); // Find A in str, return the subscript
    str.find ( "ABC"); // returns the subscript
    str.rfind ( 'A'); // Find the tail.
    str.find_first_of ( "abBc"); // Find "abBc" any character and equal str, "abBc" has returned position
    str.find_first_of ( "abBc", 1) // Find "abBc" any character and equal str, from position 1 to begin searching for "abBc" characters, "abBc" Some of return position
    str.find_last_of("abBc");

 

Guess you like

Origin www.cnblogs.com/nonames/p/12399343.html