input operations for 1.string classes(String class overloaded operator operator>> for input, same overloaded operator operator<< for output operation)
Operator is a C + + keyword that is used in conjunction with an operator to represent an operator function that should be understood as a function name as a whole operator=. This is a method of the C + + extension operator functionality. More about operator not in detail here.
String str1;
Cin >> str1; When a string is entered with cin>>, the read input of the string is stopped where the space is encountered
Cin.get (); Similar to the GetChar () function
Getline (CIN, str); String input (reading a line containing a blank string, and encountering the end of the carriage return line)
Getline (CIN, str, '! '); Enter a line of characters, assign to STR, and '! ' End
initialization of the 2.string class
string str = "Hello world!";
String str ("Hello world!");
String str1 (str,6); = = "world!"//means to take out the string from the beginning of the 6th bit to the end
String str2 (str,6,2); = = "Wo"//indicates the 2-bit string is removed from the 6th bit
Char s[]={"How is you!"};
string STR3 = S; This is permissible.
String STR4 (s);
String STR5 (s,3); = = "how"
String STR6 (s+4,s+6); = = "is"
String STR7 (5, "I"); = = "IIIII"//A String representing 5 ' I '
Str7.~string (); Destroying strings, freeing memory
character and string manipulation of the 3.string class
String str = "ABCDEF";
char ch = str[0];
char ch = str.at[0];//== ' A '//returns nth character (throws exception if out of bounds)
String str1 = "How is it?";
String str2 = "I ' m fine";
string str = STR1+STR2;
STR1 + = str2; Concatenation of 2 strings
Str.push_back ('. '); Add '. ' At the end of the string Character
Str.append ("and you"); Add "and you" to the tail of the string String
Str.append (s); Concatenate char type string s to the end of the current string
Str.append (S, 2); Connect prompt the first 2 characters of the char type string s to the end of the current string
Str.append (Str1.begin (), Str1.end ());//Connect prompt the passage between iterators to the end of the current string
Str.push_back (' K '); Connect prompt a word to the end of the current string
Str.assign ("Hello"); Re-assignment (can also be a char-character array, string strings)
Str.assign (STR1);
Char s[]={"Eee"};
Str.assign (s);
Str.assign (S, 2); n characters assigned value starting with char type string s
Str.assign (len, ch); Assigns a value to the current string with len characters ch
Str.assign (str1, 0, 3); Assigned the string to the current string, starting with 0 characters from the STR1
Str.insert (1, "ee"); Inserts a string after the specified position
String str1 = "0123456789";
Str.assign (Str1.begin (), Str1.end ());//assigned the word between iterators to the string
description of the characteristics of the 4.string class
String str= "abcdef";
int size;
Size = Str.capacity (); Return current capacity
Size = Str.max_size (); Returns the length of the largest string that can be stored in a string object
Size = Str.size (); Returns the size of the current string
Size = Str.length (); Returns the length of the current string//c++ str.length () is no different from str,size ().
BOOL Flag;
Flag = Str.empty (); Determines whether the current string is empty
int len = 10;
Str.resize (len, ch);//Set the current size of the string to Len and fill in the insufficient part with the character ch
Comparison of 5.string
Compare operations = =! = > >= < <= compare
= = = > >= < <=//can be used to compare strings directly
String str1 = "abc";
String str2 = "ACD";
String STR3 = "CCC";
Str1.compare ("abc"); Equal return 0, greater than return positive, less than return negative
Str1.compare (STR2);
Str2.compare ("C", 1); C==c return 0
Str2.compare (1,2,str3,2,1)//CD vs. C comparison
Usage: Compare the size of a string consisting of a string of B characters starting with a str1 string with a D character from the STR2 string starting with C
string of 6.string
String str;
STR1 = Str.substr (10, 15);//Returns a string consisting of 15 characters starting from subscript 10
exchange of 7.string
Str.swap (STR1);//Exchange str1 and STR values
8.string Lookup
Returns the location when the lookup succeeds, returns the value of String::npos on failure, which is-1
String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int POS;
pos = str1.find (' i ', 0);//starting from position 0 find the position of the character I in the current string
pos = Str1.find ("Ghijk", 0);//start with position 0 to find the position of the string "Ghijk" at the current string
pos = Str1.find ("OPQRSTUVW", 0, 4);//start with position 0 to find the position of the first 4 characters of the string "OPQRSTUVW" in the current string
pos = Str1.rfind (' s ', string::npos);//Find the position of the character s in the string starting from the string str1 reverse
pos = Str1.rfind ("KLMN", string::npos);//Find the position of the string "KLMN" in the string starting from the string str1 reverse
pos = Str1.rfind ("OPQRSTUVW", String::npos, 3);//From string::p the position of the first n characters in the string s from the beginning of the OS to look forward
String str = "AAAABBBBCCCCDDDEEEFFFGGGHHHIIIJJJKKLLMMMANDJFAKLSDFPOPDTWPTIOCZX";
pos = str.find_first_of (' d ', 0);//starting at position 0 Find character D where the current string first appears
pos = str.find_first_of ("Eefff", 0);//start with position 0 to find the first occurrence of the string "Eeefff" in the current string
pos = str.find_first_of ("Efff", 0, 3);//starting at position 0 to find the position of the first 3 characters in the string "Efff" in the current series
pos = str.find_first_not_of (' B ', 0);//Find the first occurrence of a character not in the string s from the current string
pos = str.find_first_not_of ("Abcdefghij", 0);//Find the first occurrence of a character not in the string s from the current string
pos = str.find_first_not_of ("Abcdefghij", 0, 3);//The position of the first character in the string that is not in the top 3 characters of the string "Abcdefghij"
The last format below is the same as first, but it is retrieved from behind!
pos = str.find_last_of (' B ', string::npos);
pos = str.find_last_of ("abcdef", String::npos);
pos = str.find_last_of ("abcdef", String::npos, 2);
pos = str.find_last_not_of (' A ', string::npos);
pos = str.find_last_not_of ("abcdef", String::npos);
pos = str.find_last_not_of ("abcdef", String::npos, 3);
Replacement of 9.string
String str = "ABCDEFGHIJKLMN";
Str.replace (0, 3, "QQQQ");//delete 3 characters starting from 0, and then insert the string "QQQQ" at 0
Str.replace (0, 3, "VVVV", 2);//delete 3 characters starting at 0, and then insert the first 0 characters of the string "VVVV" at 2
Str.replace (0, 3, "OPQRSTUVW", 2, 4);//delete 0 characters starting from 3, and then insert the string "OPQRSTUVW" 0 characters from position 2 at 4
Str.replace (0, 3, 8, ' C ');//delete 3 characters starting at 0, and then insert 0 characters C at 8
The position above can be changed to the position of the iterator, the operation is the same, here will not repeat!
insertion of 10.string, the following position can also be represented by the pointer of the iterator, the operation is the same
String str = "ABCDEFG";
Str.insert (0, "Mnop");//Insert the string "Mnop" at the beginning of the 0-bit position of the string
Str.insert (0, 2, ' m ');//At the beginning of the 0-bit position of the string, insert a 2-character m
Str.insert (0, "Uvwxy", 3);//Insert the first 3 characters in the string "Uvwxy" at the beginning of the string's 0-bit position
Str.insert (0, "Uvwxy", 1, 2);//At the beginning of the 0-bit position of the string, insert 1 characters starting from the 2-bit position of the string "Uvwxy"
Removal of 11.string
String str = "GFEDCBA";
String::iterator it;
it = Str.begin ();
it++;
Str.erase (it);//Remove the character that it points to, and return the location of the post-delete iterator
Str.erase (it, it+3);//Remove all characters between it and It+3, returning the location of the post-delete iterator
Str.erase (2);//delete all characters from the string position 3, returning the character in front of position 3
12. Stream Processing of strings
String Str ("Hello,this is a Test");
Istringstream is (str);
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;
C++string class Usage