C ++ string type details

Source: Internet
Author: User

String is a very powerful type that encapsulates string operations. Sometimes we can use string as a character container. string also supports most container operations, the following lists all the operations supported by the string type. This document is not intended to explain the usage and application of the string type, but to serve as a reference document for the string type. Each function is described in detail after annotations, you can view the information as needed.

The string operation is as follows:

Constructor:
String (); // empty string
String (size_type length, char ch); // copy (that is, length ch) of ch with length)
String (const char * str); // use str as the initial value (any length)
String (const char * str, size_type length); // same as above, the length is not limited, but do not cross the border to avoid unpredictable Problems
String (string & str, size_type index, size_type length); // a substring starting with index. The length is length or less than length.
String (input_iterator start, input_iterator end); // use the element from start to end as the initial value

Supported operators:
=
>
<
> =
<=
! =
+
+ =
[]

Append text)
Basic_string & append (const basic_string & str); // Add str at the end of the string
Basic_string & append (const char * str); // Add the c-style string pointed to by str to the end of the string
Basic_string & append (const basic_string & str, size_type index, size_type len); // Add the str substring to the end of the string. The substring starts with the index and is in The len Length.
Basic_string & append (const char * str, size_type num); // Add 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); // Add a character sequence at the end of the string, represented by the iterator start and end

Assign value (assign)
Basic_string & assign (const basic_string & str); // assign a value to the string using str
Basic_string & assign (const char * str); // assign a value to the string in the str c style
Basic_string & assign (const char * str, size_type num); // assign a value to the string using the start num character of str
Basic_string & assign (const basic_string & str, size_type index, size_type len); // assign a value to the string using the str substring. The substring starts with the index and the length is len.
Basic_string & assign (size_type num, char ch); // assign a value to the string using the num character ch

Comparison (compare)
Int compare (const basic_string & str); // compare yourself and str
Int compare (const char * str); // compare yourself and str
Int compare (size_type index, size_type length, const basic_string & str); // compare your own substrings with str. The substrings start with the index and are in length.
Int compare (size_type index, size_type length, const basic_string & str, size_type index2,
Size_type leng22.); // compare your own substrings with str's substrings. index2 and leng2are referenced by str, index, and length.
Int compare (size_type index, size_type length, const char * str, size_type leng22.); // compare your own substrings with str substrings, the substring of str starts with index 0 and its length is leng22. its own substring starts with index and its length is length.
Returned values

Less Than Zero this <str
Zero this = str
Greater than zero this> str

Delete (erase)
Iterator erase (iterator pos); // deletes the characters pointed to by the pos and returns the iterator pointing to the next character.
Iterator erase (iterator start, iterator end); // deletes all characters from start to end and returns an iterator pointing to the next position of the last character to be deleted
Basic_string & erase (size_type index = 0, size_type num = npos); // Delete the num characters starting from the index and return * this

Insert)
Iterator insert (iterator I, const char & ch); // insert a character ch before the position indicated by iterator I
Basic_string & insert (size_type index, const basic_string & str); // insert the string str into the index at the position of the string
Basic_string & insert (size_type index, const char * str); // insert the string str into the index at the position of the string
Basic_string & insert (size_type index1, const basic_string & str, size_type index2, size_type num); // index the position of the string to insert the substring of the string str (starting from index2, num characters long)
Basic_string & insert (size_type index, const char * str, size_type num); // insert num characters of the str string into the index at the position of the string
Basic_string & insert (size_type index, size_type num, char ch); // insert the copy of num-character ch in the index of the string
Void insert (iterator I, size_type num, const char & ch); // insert copy of num character ch before position indicated by iterator I
Void insert (iterator I, iterator start, iterator end); // insert a character before the position indicated by iterator I, starting from start and ending with end

Replace)
Basic_string & replace (size_type index, size_type num, const basic_string & str); // replace the characters in this string with num characters in str, starting from index
Basic_string & replace (size_type index1, size_type num1, const basic_string & str, size_type index2,
Size_type num2); // Replace the num2 characters (starting from index2) in str with the characters in this string, starting from index1 and up to num1 characters
Basic_string & replace (size_type index, size_type num, const char * str); // replace the characters in this string with num characters (starting from index) in str.
Basic_string & replace (size_type index, size_type num1, const char * str, size_type num2); // replace the characters in this string with the num2 characters in str (starting from index2, starting from index1, num1 character
Basic_string & replace (size_type index, size_type num1, size_type num2, char ch); // replace the characters in this string with num2 ch characters, starting from index, num1 character
Basic_string & replace (iterator start, iterator end, const basic_string & str); // replace the characters in this string with the characters in str, and the range indicated by the iterator start and end
Basic_string & replace (iterator start, iterator end, const char * str); // replace the content in this string with str. The range indicated by the iterator start and end
Basic_string & replace (iterator start, iterator end, const char * str, size_type num); // replace the content in this string with num characters in str, and the range indicated by start and end of iterator
Basic_string & replace (iterator start, iterator end, size_type num, char ch); // replace the content in this string with num ch characters. The range indicated by iterator start and end

Search
The various find functions are very similar, so I will not comment them out one by one.
Returned value: string: npos

Find:
Size_type find (const basic_string & str, size_type index); // return the position where str appears for the first time in the string (search from index)
Size_type find (const char * str, size_type index); // return the position where str appears for the first time in the string (search from index)
Size_type find (const char * str, size_type index, size_type length); // return the position where str appears for the first time in the string (from index to length)
Size_type find (char ch, size_type index); // returns the position of the first occurrence of the character ch in the string (from index)

Find_first_of: searches for the first character in the string that matches a character in str.
Size_type find_first_of (const basic_string & 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 );

Find_first_not_of: searches for the first character in the string that does not match the character in str.
Size_type find_first_not_of (const basic_string & str, size_type index = 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 );

Find_last_of: searches for the character string that matches the last character in str.
Size_type find_last_of (const basic_string & 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 );

Find_last_not_of: searches the string for the character that does not match the character in str.
Size_type find_last_not_of (const basic_string & str, size_type index = 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 & str, size_type index); // returns the last character that matches a character in str, starting from index
Size_type rfind (const char * str, size_type index); // returns the last character that matches a character in str, starting from index
Size_type rfind (const char * str, size_type index, size_type num); // returns the last character that matches a specific character in str, starting from index and searching for a maximum of num characters
Size_type rfind (char ch, size_type index); // returns the last ch-matched character, starting from index

At Function
Reference at (size_type index); // The at () function returns a reference to the character at the index position. if the index is not within the string range, at () reports the "out of range" error and throws an out_of_range exception.

Begin Function
Iterator begin (); // The begin () function returns an iterator pointing to the first element of the string.

End Function
Iterator end (); // returns an iterator pointing to the end of the string (next position of the last character)

C_str Function
Const char * c_str (); // returns a pointer to a regular C string with the same content as this string

Capacity Function
Size_type capacity (); // returns the number of characters that the string can hold before applying for more space. This number must be at least as large as size ().

Copy Function
Size_type copy (char * str, size_type num, size_type index); // copy your num to str (starting from index ). The returned value is the number of copied characters.

Data Functions
Const char * data (); // returns the pointer to its first character

Empty Function
Bool empty (); // If the string is null, empty () returns true (true); otherwise, false (false)

Get_allocator Function
Allocator_type get_allocator (); // return the configurator of this string

Length Function
Size_type length (); // returns the length 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 a string can store.

Rbegin Function
Rbegin (); // returns a reverse iterator pointing to the last character

Rend Function
Rend (); // returns a reverse iterator pointing to the first position of the first element.

Reserve Function
Reserve (size_type num); // retain a certain capacity to hold strings (set the capacity value)

Resize Function
Void resize (size_type num); // you can change the size of the string to num. The content of the new space is uncertain.
Void resize (size_type num, char ch); // you can specify to fill the string with ch.

Size Function
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 the current string, starting from index, with a length of num characters. If not specified, the default value is string: npos. In this way, the substr () function returns the remaining strings starting with index.
 
Swap Function
Void swap (basic_string & str); // exchange str with this string


By RO_wsy

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.