C + + String type detailed __c++

Source: Internet
Author: User

C + + String type detailed

String is a very powerful type that encapsulates the operation of strings, and sometimes we can take string as a container for characters, and string

Support for most container operations, all of the operations supported by the string type are listed below, and this article is not intended to explain the use and application of string.

Instead, you want to serve as a reference document of type string, each of which is described in detail after the comment and needs to be consulted.

1. Constructor function

string ();//empty string

string (size_type length,char ch);//copy of CH with length (that is, length ch)

string (const char *STR);// Using STR as the initial value (arbitrary length)

string (const char *str,size_type length);/ibid., length not limited, but be careful not to cross over, lest unpredictable problems occur

string (string &str , size_type index, size_type length);
A substring that begins with index, length, or less than length

string (input_iterator begin, Input_iterator end), or the element starting from start
2. Supported Operators
==
>
<
>=
<=
!=
+
+=
[  ]

3. Append text (Append)

basic_string &append (const basic_string &STR);//Add str

basic_string &append (const char *STR) at the end of the string; /Add the C-style string

basic_string &append (const basic_string &str,size_type index,size_type Len) pointed to by STR at the end of the string;
Adds a substring of STR at the end of the string, starting with the index index, Len

basic_string &append (const char *str,size_type num); Add the num characters in STR to the end of the string

basic_string &append (size_type num,char ch);//Add num characters to the end of the string ch

basic_string &append (input_iterator start,input_iterator end);
Adds a character sequence push_back (' K ') that is represented by the iterator start and end at the ends of the string

;//Fonts A word to the end of the current string

4. Assign value (assign)

basic_string &assign (const basic_string &STR);//Use STR to assign a value to a string

basic_string &assign (const char *STR); Assign a value

basic_string &assign (const char *str,size_type num) to the string in str c style;//Assign value to a string with the start num character of str

basic_string &assign (const basic_string &str,size_type index,size_type len);
Assigns a string to a substring of STR, starting with the index index, Len

basic_string &assign (size_type num,char ch);//Assigning value

to string with num character ch String &assign (const_iterator begin,const_itertor end);
Assign a section between the primary and last iterators to a string

5. Comparison (Compare)

int compare (const basic_string &STR);//Compare yourself and str

int compare (Size_type index,size_type, Length,const basic_ String &str);
Compare own substring and STR, SUBSTRING begins with index index, length

int compare (size_type index,size_type length,const basic_string &str, Size_type 
            index2,size_type length2);
Compare your own substring and str substring, where index2 and length2 reference str,index and length references themselves

int compare (const char *STR);//Compare yourself and str

int Compare (int pos, int n,const char *s)
//Compare own substring, start with POS, n characters, and s to compare

int compare (Size_type index,size_type Length,const Char *str,size_type length2);
Compare your own substring with the substring of STR, where the substring of STR begins with index 0, length is length2, and its substring
//begins with index, length

return value condition

Less than 0 this < str
0 this = = str
Greater than 0 this > str

6. Delete (erase)

Iterator Erase (iterator, iterator last);
Deletes all characters between [First,last], returns the position of the post-deletion iterator

iterator erase (iterator it);//Deletes the character that it points to, returns the location of the deleted iterator

string &erase (int pos = 0, int n = npos);//delete the N-character at the beginning of the POS, return the modified string

7. Inserting (insert)

Iterator Insert (iterator i,const char &ch);//Inserts a character in front of the position in iterator I

, ch basic_string &insert (size_type index, Const basic_string &STR);//At the position of the string index inserts a string str

basic_string &insert (size_type index,const char *str);/ At the position of the string index inserts the string str

basic_string &insert (size_type index1,const basic_string &str,size_type, Size_type num);
In the position of the string index inserts the substring of the string str (starting from INDEX2, the long num characters)

basic_string &insert (size_type index,const Char *str,size_ Type num);
In the position of the string index inserts the num character of the string str

basic_string &insert (size_type index,size_type num,char ch);
At the position of the string, index inserts a copy of the NUM character ch

(iterator i,size_type num,const char &ch);
Insert the copy void insert of num character Ch before the position represented by iterator I

(iterator i,iterator begin,iterator end);
Inserts a character in front of the position represented by iterator I, starting at start, ending with end

8. Replacement (replace)

Basic_string &replace (size_type index,size_type num,const basic_string); Replaces the characters in this string with the num characters in STR, starting with index replace (size_type index1,size_type num1,const basic_string &str,size_type
Index2,size_type num2); Replaces the characters in this string with the num2 character (starting from index2) in Str, starting with index1, up to NUM1 characters basic_string &replace (Size_type num,
const char *STR); Replaces the characters in this string with the NUM characters in STR (starting with index) basic_string &replace (size_type index,size_type num1,const Char *str,size_
Type num2); Replaces the characters in this string with the num2 character (starting from index2) in Str, starting with index1, num1 characters basic_string &replace (Size_type index,size_type,
Size_type Num2,char ch); Replaces characters in this string with num2 ch characters, starting with index, NUM1 characters basic_string &replace (iterator start,iterator end,const basic_string
&AMP;STR);
Replaces the characters in this string with the characters in Str, and the iterator start and end indicates the range basic_string &replace (iterator start,iterator end,const char *str); Replaces the contents of this string with STR, the iterator start and end indicates the range basic_string &replace (iterator start,iterator end,const char *str,size_type num
); Replaces the inner of this string with the NUM characters in strCapacity, iterator start and end indicator range basic_string &replace (iterator start,iterator end,size_type ch); Replaces the contents of this string with the NUM CH character, and the iterator start and end indicates the range

9. Find

function Find:size_type Find (const basic_string &AMP;STR, size_type index);
Returns the position in which Str first appears in the string (starting at index) Size_type find (const char *STR, size_type index);
Returns the position at which Str first appears in a string (starting at index) Size_type find (const char *STR, size_type index, size_type length);
Returns the position at which Str first appears in the string (starting at index, length) size_type find (char ch, size_type index); Returns the position in which the character ch first appears in a string (find from index) function find_first_of: Finds the first character in a string that matches a character in Str size_type find_first_of (const basic_s

    Tring &str, Size_type index = 0);

    Size_type find_first_of (const char *STR, Size_type index = 0);

    Size_type find_first_of (const char *STR, SIZE_TYPE index, Size_type num);

Size_type find_first_of (char ch, size_type index = 0); Function Find_first_not_of: Finds the first character in a string that does not match the characters in STR size_type find_first_not_of (const basic_string &AMP;STR, Size_type in

    Dex = 0);

    Size_type find_first_not_of (const char *STR, Size_type index = 0); Size_type find_first_not_of (const char *STR, SIZE_TYPE index, Size_type num);

Size_type find_first_not_of (char ch, size_type index = 0); Function find_last_of: Finds the last character in the string that matches one of the characters in STR size_type find_last_of (const basic_string &AMP;STR, size_type index = NPOs

  );

  Size_type find_last_of (const char *STR, size_type index = NPOs);

  Size_type find_last_of (const char *STR, SIZE_TYPE index, Size_type num);

Size_type find_last_of (char ch, size_type index = NPOs); Function find_last_not_of: Finds the last character in the string that does not match the characters in STR size_type find_last_not_of (const basic_string &AMP;STR, Size_type IND

    ex = NPOs);

    Size_type find_last_not_of (const char *STR, size_type index = NPOs);

    Size_type find_last_not_of (const char *STR, SIZE_TYPE index, Size_type num);

Size_type find_last_not_of (char ch, size_type index = NPOs);
  RFind function Size_type rfind (const basic_string &AMP;STR, size_type index);
  Returns the last character that matches one of the characters in Str, starting with index to find Size_type rfind (const char *STR, size_type index); Returns the last character that matches one of the characters in Str, starting with index to find Size_tyPE rfind (const char *STR, SIZE_TYPE index, Size_type num);
  Returns the last character that matches one of the characters in Str, starting at index and finding up to num characters size_type rfind (char ch, size_type index); Returns the last character that matches the CH, starting from index


10. Other Functions

At function Reference at (size_type index); The at () function returns a reference to the character at the index position. If index//is not within the string range, at () reports an "out of range" error and throws the Out_of_range exception begin function iterator begin (); the//begin () function returns an iterator that points to a string The first element end function, iterator, returns an iterator that points to the ending of the string (the next position of the last character) c_str function const char *c_str ();//Returns a pointer to the normal C string, which is the contents of the String the same capacity function Size_type capacity ()//returns the number of characters that the string can//will hold before the additional space is requested.
    This number is at least as large as size () copy function size_type copy (char *str, size_type num, size_type index); Copies its own num characters into STR (starting with index). The return value is the number of characters that are copied the data function const char *data ()//Returns a pointer to its first character empty function bool empty ();//If the string is empty then empty () returns True (true), otherwise it returns False (FAL SE) get_allocator function allocator_type get_allocator ()//Returns the Configurator length function Size_type length () of this string, or the lengths of the string. This number should be the same as the number returned by size () max_size size_type max_size ();//returns the maximum number of characters that can be saved by the string Rbegin function Rbegin ();//Returns a reverse iterator pointing to the last character rend The function rend ()//Returns a reverse iterator that points to the first position of an element, Reserve function reserve (Size_type num);//reserve a certain capacity to hold a string (set capacity value) Resize letterNumber of void resize (Size_type num); Change the size of this string to num, the contents of the new space are not certain void resize (size_type num, char ch);//can also specify the size function to be filled with CH
     Size ()//returns the number of characters in the string substr function basic_string substr (size_type index, size_type num = NPOs); Returns a substring of this string, starting at index, and having a long num character. If not specified,//will be the default value String::npos. In this way, the substr () function simply returns the remaining string swap function void (basic_string &AMP;STR) that starts at index, and/or the STR and this string.

11. Example

#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 Description of the feature    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);//Assignments The string STR7 3 characters from 0 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)//judge 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);//comparison str10 the size of the 12-character string from 6 and the STR9 string of 3 characters starting at 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 character I from position 0The position of the current string cout << pos << Endl;
    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 of the string s in the current string, starting with the string::p the OS cout << POS <& Lt

    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);//starting at position 0 to find the first in the current string"Efff" consists of the first 3 characters in the array of characters in the position of the cout << pos << 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 <<
    POS << Endl;
    The following last format is consistent with the last, except that it is 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 0 characters from position 2 cout << str14 << End
    L
    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 from 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, return the location 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;
System ("pause");
 }


Reference Blog: http://blog.csdn.net/ro_wsy/article/details/7701218


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.