The String class in the C standard library is used to discard the string class in the C standard library, because he compares to the former, do not worry about whether the memory is sufficient, the string length, etc., and as a class, his integrated operation function It is enough to complete the needs of our most cases (even 100%). We can use = assignment operation, == compare, do series (is it very simple?). We don't think it as a basic data type of C . Ok, enter the topic ......... First, in order to use the string type in our program, we must include the header file
a) =, assign () // assignment B) SWAP () // Exchange two strings content C) =, append (), push_back () // Add character d) insert () // Insert Character E) ERASE () // Remove Character F) CLEAR () // Delete All Character G) Replace () // Replace Character H) // Series Strings I) ==,! =, <, <=,>,> =, compare () // Comparison String J) Size (), Length () // Return Character number K) Max_Size () // Return Character Maximum number L) EMPTY () / / Judgment whether the string is empty M) Capacity () // Returns the character capacity N) RESERVE () // retains a quantity of memory to accommodate a certain number of characters O) [], AT () // Access Single characters P) >>, getLine () // read a value from STREAM Q) << // Write the conspiracy to write STREAMR) COPY () // assigns a value to a c_strings) c_str () // will The content returns T) data () // returns the content in the form of a character array, returns a sub-string V) find function w) begin () end () // provides an iterator similar to STL Support X) Rbegin () rend () // Reverse iterator Y) GET_ALLOCATOR () // Return Configurator Next Detailed Description: 2.1 C strings and c supplied by C strings to correspond The method of c_string is to use data (), c_str (), and copy (), where DATA () returns the character string in the form of a character array, but does not add '/ 0'. C_STR () returns a character array ending with '/ 0', and copy () copys the contents of the string or writes the existing c_string or character array. The C string is not ending with '/ 0'. My suggestion is to use the C string in the program, unless you have to use c_string. Since it is just a brief introduction, detail the brush, who wants to learn more about the precautions to give me a message (to my inbox). I explained in detail. 2.2 Size and Capacity Functions A C string There are three sizes: a) existing characters, function is size () and length (), they are equivalent. EMPTY () is used to check if the string is empty. b) max_size () This size refers to the number of characters that can be included in the current C string, which is likely to have a relationship with the size of the machine itself or the location of the string. We don't have to care if you care about him, and we should use it enough. However, if you are not enough, you will throw Length_Error anomaly C) Capacity () reassigns the maximum number of characters that String can contain before the memory. Another need to point out this is a reserve () function, this function is reassigned for String. The size of the redistribution is determined by its parameters, the default parameter is 0, this time the String is unmodified and reduced.
There is also a need to repeat the problem of C strings and c-string conversion. Many people will encounter such problems, what they do should call others' functions, what is the class (such as database connection functions Connect (char *, char *)), But the function parameters of others are in the form of char *, and we know that the character array returned by the c_str () and Data () is owned by the string, so it is a const char *, to be the above The parameters of the functions mentioned, must also copy to a char *, and our principle is not used without using the C string. So, then our way of handling is: If this function does not modify the contents of the parameter (that is, char *), we can do CNECT ((char *) userid.c_str (), (char *) passwd.c_str ), But this is dangerous, because the strings after this conversion are actually modified (interested in trying themselves), so I emphasize that the parameters are not modified when the non-function call is changed, otherwise you must copy Go to a char *. Of course, a more secure approach is to copy to a char * in any case. At the same time, we also pray that the masters that still use C strings (say they are masters, maybe they start programming when we also wear the trousers, haha ...) is more specific. If we don't have to force the forced conversion. 2.3 Elements Access We can use the subscript operator [] and function AT () to access the character contained. However, it should be noted that operators [] do not check if index is valid (effective index 0 ~ str.length ()), if index fails, will cause undefined behavior. And at () will check that if the index is invalid if the index is used in AT (), it will throw out_of_range exception. There is an exception to say that const string a; the operator [] for the index value is A.Length () is still valid, its return value is '/ 0'. All other situations, A.Length () index is invalid. Examples are as follows: const string cstr ("const string"); String Str ("string"); str [3]; //okstr.at(3); // okstr [100]; // Unexpected behavior STR. AT (100); // throw out_of_rangeStr [str.Length ()] // Unefained behavior cstr [cstr.length ()] // Return '/0'str.at(Str.Length ());//throw OUT_OF_RANGECSTR.AT (cstr.length ()) throw out_of_range I don't agree similar to the reference or pointer assignment: char & r = s [2]; char * p = & s [3]; because of the reassign, R, P Exposer immediately. The method of avoiding is not used. 2.4 Compare Functions C strings support common comparison operators (>,> =, <=, ==,! =), And even support String and C-String comparisons (such as Str <"Hello"). In use>,> =, <, <= these operators are based on "Current Character Features" to compare characters according to the order of dictionary. The character is small in front of the dictionary, and the order of comparison is from the front to rearward, and the unequal character is encountered by the comparison result of the two characters in this location, determine the size of the two strings. At the same time, String ("AAAA") Another powerful comparison function is a member function Compare (). He supports multi-parameter processing, supports comparison with index values and length positioning substrings. He returned an integer to represent the comparison result, the return value is as follows: 0 - equal> 0- greater than <0- less than. For example: String S ("ABCD"); S.compare ("ABCD"); // Returns 0 S.comPare ("DCBA"); // Returns a value of less than 0 S.Compare ("ab"); / / Returns a value of greater than 0 S.Compare (s); // equal S.Compare (0, 2, s, 2, 2); // is compared to zero S.comPare with "ab" and "cd". 1, 2, "BCX", 2); // Compare with "BC" and "BC". how about it? Enter enough! what? Can't you meet your appetite? Ok, then wait, there is a more personal comparative algorithm. First give a prompt, use the STL comparison algorithm. what? Don't you do a word for STL? Rely, you re-press! 2.5 Change the content This is a large part of the operation of the string. First, the value is assigned, and the first assignment method is of course used to use operators =, the new value can be string (such as: s = ns), c_string (such as: s = "gaint") or even single characters (such as: s = 'J' ). You can also use member functions Assign (), which makes you more flexible to assign a string. Still Example: S.ssign (STR); // Do not say S.ssign (STR, 1, 3); // If the Str is "IAMANGEL" is to assign "AMA" to string S.ssign (Str, 2, string :: npos); // Put the string STR from the index value 2 to the end to the end of SS.Assign ("gaint"); // Do not say s.ssign ("nico", 5); // 'n' 'I' '' '' '/ 0' gives a string S.ssign (5, 'x'); // There are three methods that assign the five X to the string to empty the string: s = ""; s.clear (); S.RASE (); (I am more and more feeling more than speaking, making others easy to understand!). String provides a lot of functions for insertion (Insert), delete, replace, increasing characters. Let me talk about adding characters (hereafter, the increase is on the tail), the function is =, append (), push_back (). For example: S = Str; // Add a string S = "my name is jiayp"; // plus a c-string S = 'a'; // add a character S.Append (STR); S.Append (STR, 1, 3); // Does not explain the explanation of the function parameters asSign in S.Append (STR, 2, String :: Npos) // does not explain S.Append ("My Name IS JiayP") S.Append ("nico", 5); S.Append (5, 'x'); s.push_back ('a'); // This function can only increase the understanding of STL familiarity to STL may be simple. You need to insert a string in a location in the middle of String, then you can use the insert () function, this function requires you to specify an index of an installation position, and the string being inserted will place behind this index. S.insert (0, "my name"); sinsert (1, str); this form of Insert () function does not support incoming single characters, the single character must be written in the form of string (people disgusting ). Since you feel disgusting, then you have to continue to read the following: Insert a single character, the insert () function provides two overload functions that insert a single character operation: Insert (size_type index, size_type num, chart c) and INSERT (Iterator POS, SIZE_TYPE NUM, CHART C). Where SIZE_TYPE is an unsigned integer, Iterator is char *, so you call the insert function: Insert (0, 1, 'j'); which one will convert to which one is the first parameter? So you must write this: insert (String :: size_type) 0, 1, 'j')! The second form points out the form of using iterators, which will be mentioned later. By the way, String has a lot of operations to use STL's iterator, and he tries to do with STL. There are several types of delete functions ERASE () (I am quite annoying!), And there are several replacement functions Replace (). Example: String S = "IL8N"; S.Replace (1, 2, "nternationalizatio); // From 2 replacements starting from the index 1 to the back c_strings.rase (13); // start from the index 13 After deleting S.RASE (7,5); // Delete 5 2.6 Removal Subtrings and strings from Index 7 and strings and strings are the functions of substrs: substr (), form as follows: s. Substr (); // Returns all content s.SUBSTR (11); // From the index 11 subsequent substrings S.SUBSTR (5, 6); // From the index 5 start 6 characters to put two characters The function combined with the string is . (Who does not understand, please call 120) 2.7 Enter the output operation 1. >> Read a String from the input stream. 2. << Write a String to the output stream. Another function is getLine (), he reads a line of content from the input stream until the branch is encountered or the file end. 2.8 Search and Find Find functions, the function is also very powerful, including: find () rfind () Find_First_of () Find_Last_OF () Find_First_not_OF () Find_last_not_of () These functions returns the first character in the character interval that meets the search criteria The index of characters, returns NPOS without finding the target. The parameters of all functions are as follows: The first parameter is the object being searched. The second parameter (not available) indicates the starting point index within the String, and the third parameter (not available) indicates the number of characters in the search. It's relatively simple, not much to say that you can ask me, I will answer it again. Of course, more powerful STL search will be mentioned later. Finally, the meaning of NPOS, String :: npos type is string :: size_type, so once this index value must be string :: size) TYPE type, more cases We can compare functions and NPOS directly (such as: if ("JIA") == String :: Npos). The second part is about the support of the C string to iterators. I will write it as needed (meaning that it means no need, I am happy, haha ...).