A detailed explanation of the string class in C + +

Source: Internet
Author: User
Tags int size

The string class provided in standard C + + is also very powerful and can generally be used when we develop projects. Now the specific use of a part of the list below, only to play a role in the bar, well, less nonsense, directly to the point of it.
To use the string class in standard C + +, you must include the
#include <string>//attention is <string&gt, not <string.h&gt, with. h is the header file in C language.
Using Std::string;
Using Std::wstring;
Or
using namespace Std;


Most functions of the string class:
Begin to get the iterator that points to the beginning of the string
End gets the iterator that points to the ending of the string
Rbegin gets the iterator that points to the beginning of the reverse string
Rend gets the iterator that points to the end of the reverse string
Size gets the sizes of the strings
The length and size functions function the same
Maximum possible size of max_size string
Capacity the possible size of a string without reallocating memory
Empty to determine if it is empty
Operator[] takes a few elements, equivalent to an array
C_str get C-style const char* string
Data gets the string content address
operator= assignment operator
Reserve Reserve Space
Swap swap function
Insert Caret character
Append append characters
Push_back Append characters
operator+= + = operator
Erase Delete String
Clear clears all contents of character container
Resize Space reallocation
Assign is the same as the assignment operator
Replace replace
Copy String to Space
Find Lookup
RFind Reverse Lookup
Find_first_of finds any character in a substring, returns the first position
Find_first_not_of find any character that does not contain a substring, return to the first position
Find_last_of finds any character in a substring, returns the last position
Find_last_not_of find any character in a substring, and return to the last position
SUBSTR Get string
Compare comparison string
operator+ string link
operator== judge whether the equality
Operator!= judgment is not equal to
operator< is judged to be less than
Operator>> reads a string from the input stream
operator<< string writes to output stream
Getline read a row from the input stream






function prototypes for string classes:


Constructor of String class:
String (const char *s); Class with C string S.
string (int N,char c); Class with N-character C
In addition, the string class also supports default constructors and copy constructors, such as String s1;string s2= "Hello," which is the correct notation. A Length_error exception is thrown when the constructed string is too long to be expressed;


Character operations for string classes:
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, but the at function provides a range check and throws a Out_of_range exception when the bounds are crossed, and 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 n characters in the current string starting with Pos to an array of characters starting with S, returning the number of actual copies


Description of the attribute of string:
int capacity () const; Returns the current capacity (that is, the number of elements in string that do not need to increase 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 in the insufficient part with character C
Input and output operations for the string class:
The string class overload operator operator>> is used for input, and the same overloaded operator operator<< is used for output operations.
function Getline (IStream &in,string &s), which is used to read a string from input stream in to s, separated by a newline character ' \ n '.
Assignment of String:
String &operator= (const string &s);//Assign the string s to the current string
String &assign (const char *s);//Assign value with C type string s
String &assign (const char *s,int n);//n-word assignments value starting with C string s
String &assign (const string &s);//Assign the string s to the current string
String &assign (int n,char c);//Assign value to the current string with n characters c
String &assign (const string &s,int start,int N);/to assignments the current string with n words from start in the string s
String &assign (Const_iterator first,const_itertor last);//Assign the part between the primary and last iterators to the string
String's connection:
String &operator+= (const string &s);//Connect the 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);//The first n words of the C type string s are fonts to the end of the current string
String &append (const string &s); With operator+= ()
String &append (const string &s,int pos,int N) fonts to the end of the current string with n words starting from POS in string s
String &append (int n,char c); Add n characters at the end of the current string C
String &append (Const_iterator first,const_iterator last);//Connect the portion of the iterator between the primary and last to the end of the current string


Comparison of String:
BOOL operator== (const string &s1,const string &s2) const;//compare two strings for equality
Operators ">", "<", ">=", "<=", "!=" are overloaded for string comparisons;
int compare (const string &s) const;//compares the current string and the size of s
int compare (int pos, int n,const string &s) const;//Compare the current string of n characters from Pos to the size of s
int compare (int pos, int n,const string &s,int pos2,int n2) const;//compares the current string of n characters from the beginning of a POS string with s
The size of the string consisting of N2 characters beginning 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;
Compare function returns 0 when 1,< returns -1,== at >


Substring of string:
String substr (int pos = 0,int n = NPOs) const;//return of N-character strings starting with Pos
Exchange of String:
void Swap (string &s2); Swap the value of the current string and S2


Lookup function for String class:
int find (char c, int pos = 0) const;//Look for the character C at the position of the current string from the POS
int find (const char *s, int pos = 0) const;//The position of the string s in the current string starting from Pos
int find (const char *s, int pos, int n) const;//The position of the first n characters in the string s in the current string starting from the POS
int find (const string &s, int pos = 0) const;//The position of the string s in the current string starting from Pos
Returns the location of the String::npos when the lookup succeeds, and the failure returns the value of the
int RFind (char c, int pos = NPOs) const;//Find the position of the character C in the current string from the beginning of the POS
int rfind (const char *s, int pos = NPOs) const;
int rfind (const char *s, int pos = NPOs, int n) const;
int RFind (const string &s,int pos = NPOs) const;
From the beginning of the POS find the position of the first n characters in the string s in the current string, successfully return to the position, and return the value of String::npos when the failure occurs
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 &s,int pos = 0) const;
Starts from the POS to find the position of the first character in the current string in an array of the top n characters of S. Lookup failed return 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;
Finds the first occurrence of a character that is not in string s from the current string, and fails back to 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 (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, just looking forward from the back


substitution function for String class:
String &replace (int p0, int n0,const char *s)//delete p0 characters starting from N0, then insert string s at P0
String &replace (int p0, int n0,const char *s, int n);//delete p0 characters that are starting with the, and then insert the first n characters of the string s at N0
String &replace (int p0, int n0,const string &s)//delete p0 characters starting from N0, then insert string s at P0
String &replace (int p0, int n0,const string &s, int pos, int n);//delete p0 start n0 characters, then insert n characters from Pos in string s at P0
String &replace (int p0, int n0,int n, char c);//delete n0 characters starting p0, then insert n characters at P0
String &replace (iterator first0, iterator last0,const char *s);//replace the portion between [first0,last0) with the string s
String &replace (iterator first0, iterator last0,const char *s, int n);//Replace the part between [First0,last0] with the first n characters of S
String &replace (iterator first0, iterator last0,const string &s);//Replace the part between [First0,last0) with string s
String &replace (iterator first0, iterator last0,int N, char c);//replace the portion between [first0,last0) with n characters c
String &replace (iterator first0, iterator last0,const_iterator-I, const_iterator last);//Put [First0, LAST0) is replaced by a string between [First,last]


Insert function for 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 4 functions insert the first n characters of the POS in the string s at the P0 position
String &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 the inserted post iterator
void Insert (iterator it, Const_iterator, 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 function for String class
Iterator Erase (iterator primary, iterator last);//delete all characters between [First,last], return the position of the post-deletion iterator
Iterator Erase (iterator it); Remove the character that it points to, and return the position of the post-deletion iterator
String &erase (int pos = 0, int n = npos);//delete the N-character at the beginning of the POS, return the modified string


Iterator processing for string classes:
The string class provides iterator iterator for forward and backward traversal, which provides the syntax to access individual characters, similar to pointer operations, without the iterator checking the scope.
Declaring an iterator variable with string::iterator or String::const_iterator, Const_iterator does not allow changes to the contents of the iteration. Common iterator functions are:
Const_iterator begin () const;
Iterator begin (); Returns the starting position of a string
Const_iterator end () const;
Iterator End (); Returns the position after the last character of a string
Const_iterator rbegin () const;
Iterator Rbegin (); Returns the position of the last character of a string
Const_iterator rend () const;
Iterator rend (); Returns the front of the first character position of a string
Rbegin and rend are used for iterative access from backward forward, by setting the iterator String::reverse_iterator,string::const_reverse_iterator implementation


String Stream Processing:
By defining Ostringstream and Istringstream variable implementations, #include <sstream> header files
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 ();


    A detailed explanation of string function usage.
    Attached code, write specific usage.


    #include <iostream> #include <string> #include <sstream> using namespace std;
        int main () {//1.string class overloaded operator operator>> for input, same overloaded operator operator<< for output operation string str1;
        CIN >> str1;//When using cin>> to enter a string, the place where the space is encountered is to stop the read input of the string cout << str1 << Endl;
        The function of the cin.get ()//is to read the terminator of the cin>> input without affecting the input from the getline.


        Getline (CIN, STR1);//String Line input cout << str1 << Endl;

        2.string class constructor String str2 = "AAAAA";//simplest string initialization cout << str2 << Endl;
        Char *s = "BBBBB";

        String Str3 (s);///cout << STR3 << Endl with C string s;
        char ch = ' C ';

        String STR4 (5, CH);///N-character ch initialization cout << STR4 << Endl;
        Character manipulation of the 3.string class string str5 = "ABCDE";

        CH = str5[3];//operator[] Returns the position of nth characters in the current string cout << ch << endl;String STR6 = "ABCDE";
        ch = str6.at (4);//at () returns the position of the nth character in the current string and provides a range check that throws an exception when it crosses the line.

        cout << ch << endl;
        4.string Feature description string str7 = "ABCDEFGH";
        int size;
        Size = str7.capacity ()//return current capacity cout << size << Endl;
        Size = Str7.max_size ()//returns the length of the maximum string that can be stored in a string object cout << size << Endl;
        Size = Str7.size ()//returns the current string cout << sizes << Endl;
        Size = Str7.length ()//returns the length of the current string cout << size << Endl;
        BOOL Flag;
        Flag = Str7.empty ()//To determine whether the current string is empty cout << flag << Endl;
        int len = 10;

        Str7.resize (len, ch);//Set the current size of the string to Len, and fill the insufficient portion of the character ch cout << STR7 << Endl;
        5.string assignment string str8;
        Str8 = str7;//Assigns string str7 to the current string cout << str8 << Endl;
        Str8.assign (STR7)//Assign the string str7 to the current string cout << str8 << Endl; StR8.assign (s);//cout with C type string s << str8 << Endl;
        Str8.assign (S, 2);//assignments value of n characters starting with C type string s cout << str8 << Endl;
        Str8.assign (len, ch);//with Len character Ch assigned to current string cout << str8 << Endl;
        Str8.assign (STR7, 0, 3)//The 3 words from 0 in the string STR7 assignments to the current string cout << str8 << Endl;
        String STR9 = "0123456789";

        Str8.assign (Str9.begin (), Str9.end ());//The word between the iterators assignments to the string cout << str8 << Endl;
        6.string connection string Str10;
        Str10 + + str9;//string STR9 connected to the end of the current string cout << str10 << Endl;
        Str10.append (s);//Connect the C type string s to the end of the current string cout << str10 << Endl;
        Str10.append (S, 2);//The first 2 characters of the C type string s fonts to the end of the current string cout << str10 << Endl;
        Str10.append (Str9.begin (), Str9.end ())//A paragraph of the iterator fonts to the end of the current string cout << str10 << Endl; Str10.push_back (' k ');//Fonts A word to the end of the current string cout << Str10<< Endl;
        7.string Comparison flag = (STR9 = = STR10);//Determine whether two strings are equal cout << flag << Endl;
        Flag = (STR9!= str10)//judge whether two strings are unequal cout << flag << Endl;
        Flag = (Str9 > Str10);//Determine whether two strings are greater than the relationship cout << flag << Endl;
        Flag = (Str9 < STR10);//Determine whether two strings are less than the relationship cout << flag << Endl;
        Flag = (STR9 >= str10);//Determine if two strings are greater than or equal to the relationship cout << flag << Endl;

        Flag = (STR9 <= str10);//judge whether two strings are less than or equal to the relationship cout << flag << Endl;
        The following 3 functions are also applicable to C-type strings, returning 0 flag = Str10.compare (STR9) when returning 1,< at > in the Compare function, and/or comparing the size of two strings by subtracting ASCII.
        cout << flag << Endl;
        Flag = Str10.compare (6, STR9);//Compare Str10 string of 12 characters starting from 6 with STR9 size cout << flag << Endl; Flag = Str10.compare (6, STR9, 3, 5);//Compare Str10 string of 12 characters starting with 6 and the size of the STR9 string of 3 characters starting with 5 cout;

        Flag << Endl;
        A string of 8.string strings Str11;

        Str11 = Str10.substr (10, 15);//Returns a string of 15 characters starting with subscript 10 cout << str11 << Endl;

        9.string Exchange Str11.swap (STR10)///Exchange Str11 and Str10 value cout << str11 << Endl;
        10.string Lookup, find a successful return to the location, failure to return String::npos value, that is -1 string str12 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int POS;
        pos = str12.find (' i ', 0);//Find the character I at the current string position cout << pos << Endl from position 0;
        pos = Str12.find ("Ghijk", 0);//start with position 0 to find the string "Ghijk" at the current string position cout << pos << Endl; 
        pos = Str12.find ("OPQRSTUVW", 0, 4)//from position 0 to find the position of the string "OPQRSTUVW" with the first 4 characters in the current string cout << pos << Endl;
        pos = Str12.rfind (' s ', string::npos),//from the string str12 reverse start find the position of the character s in the string cout << pos << Endl;
        pos = Str12.rfind ("KLMN", String::npos)//from the string str12 reverse start find the position of the string "KLMN" in the string cout << pos << Endl; pos = Str12.rfind ("OPQRSTUVW", String::npos, 3); The position of the first n characters in the string s in the current string, starting from the string::p the OS cout << POS &LT;&L T

        Endl
        String str13 = "AAAABBBBCCCCDDDEEEFFFGGGHHHIIIJJJKKLLMMMANDJFAKLSDFPOPDTWPTIOCZX";
        pos = str13.find_first_of (' d ', 0);//starting at position 0 find the first occurrence of character D in the current string cout << pos << Endl;
        pos = str13.find_first_of ("Eefff", 0)//starting at position 0 to find the position where the string "Eeefff" first appears in the current string cout << pos << Endl; pos = str13.find_first_of ("Efff", 0, 3); start at position 0 to find the position of the first character in the current string that is in the top 3 characters of the string "Efff" cout << pos <&lt ;
        Endl
        pos = str13.find_first_not_of (' B ', 0);//Find the position of the first character not in string s in the current string cout << pos << Endl;
        pos = str13.find_first_not_of ("Abcdefghij", 0); Find the position of the first character not in string s in the current string cout << pos << Endl; pos = str13.find_first_not_of ("Abcdefghij", 0, 3); Finds the first occurrence of characters in a string that is not composed of the top 3 characters of the string "Abcdefghij" in the current string cout <& Lt
        POS << Endl; The format of the following lastIt's consistent with the beginning, but it's retrieved from behind.
        pos = str13.find_last_of (' B ', string::npos);
        cout << pos << Endl;
        pos = str13.find_last_of ("abcdef", String::npos);
        cout << pos << Endl;
        pos = str13.find_last_of ("abcdef", String::npos, 2);
        cout << pos << Endl;
        pos = str13.find_last_not_of (' A ', string::npos);
        cout << pos << Endl;
        pos = str13.find_last_not_of ("abcdef", String::npos);
        cout << pos << Endl;
        pos = str13.find_last_not_of ("abcdef", String::npos, 3);

        cout << pos << Endl;
        11.string Replacement string str14 = "ABCDEFGHIJKLMN";
        Str14.replace (0, 3, "QQQQ");/delete 3 characters starting from 0, then insert the string "QQQQ" cout << str14 << endl at 0;
        Str14.replace (0, 3, "VVVV", 2)//delete 3 characters starting from 0, then insert the first 0 characters of the string "VVVV" in 2 cout << str14 << Endl; Str14.replace (0, 3, "OPQRSTUVW", 2, 4);//delete 0 characters starting from 3, then insert the string "OPQRSTUVW" from the bit at 0Set 2 start of 4 characters cout << str14 << Endl;
        Str14.replace (0, 3, 8, ' C ');/delete 3 characters starting from 0, then insert 0 characters C cout << str14 << Endl in 8;

        The above position can be changed to the position of the iterator, the operation is the same, here will not repeat.
        12.string insertion, the following position can also be represented by an iterator pointer, the operation is the same as String str15 = "ABCDEFG";
        Str15.insert (0, "Mnop"),//at the 0-bit start of the string, insert the string "Mnop" cout << str15 << Endl;
        Str15.insert (0, 2, ' m ');//At the beginning of the 0 position of the string, insert 2 characters m cout << str15 << Endl;
        Str15.insert (0, "Uvwxy", 3); Inserts the first 3 characters in the string "Uvwxy" cout << str15 << Endl at the 0-bit start of the string;

        Str15.insert (0, "Uvwxy", 1, 2); At the beginning of the 0 position of the string, insert 1 characters starting with the 2 position of the string "Uvwxy" cout << str15 << Endl;
        13.string Delete String str16 = "GFEDCBA";
        String::iterator it;
        it = Str16.begin ();
        it++;
        Str16.erase (it); Deletes the character that it points to, returns the location of the cout after the deletion of the iterator << str16 << Endl; Str16.erase (it, it+3);//delete all characters between it and It+3, returning the position of the post-deletion iterator
        cout << str16 << Endl;

        Str16.erase (2)//delete all characters from string position 3, return the character cout << str16 << Endl before position 3;
        14. Stream Processing string Str17 ("Hello,this is a Test");
        Istringstream is (STR17);
        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 () << Endl;
 }





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.