C + + String type details

Source: Internet
Author: User

String is a very powerful type, well encapsulates the operation of a string, sometimes we can put string as a container of characters, string also supports most container operations, the following lists all the operations supported by the string type, This article is not intended to explain the use and application of string, but rather as a reference document of type string, each function is described in detail after the comment, which is required to be consulted.

The string operation is as follows:

constructor function:
   
string ();//empty string    (size_type length, char ch);//copy of CH with length (that is, length ch)    string (const char *STR);// The initial value of str (any length)    string (const char *STR, size_type length);//Ibid., but do not cross the bounds to avoid unpredictable problems.    string (String &str , size_type index, size_type length);//The substring starting with index, length is long, or less than length    string (input_iterator start, input_ iterator end);//The initial value of the element from start to end



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

Append text (Append)
  
basic_string &append (const basic_string &STR);//Add str basic_string &append at the end of the string   (const char *STR); /Add the C-style string pointed to by STR at the end of the string   basic_string &append (const basic_string &STR, Size_type index, Size_type len);// Add a substring of STR at the end of the string, starting with the index index and the length Len   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 sequence of characters at the end of the string that is represented by the iterator start and end



Assign value (assign)
  
basic_string &assign (const basic_string &STR);//Use STR to assign a string value    basic_string &assign (const char *STR);// Assigns a string value    basic_string &assign (const char *STR, size_type num) in STR c style, and//assigns a string value to the starting num character of str    basic_string & Amp;assign (const basic_string &STR, Size_type index, Size_type len), or//string assignment with a substring of STR, starting with the index index and the length Len    basic_string &assign (size_type num, char ch);//Assign a value to a string with num characters 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 own substrings and STR, the substring starts with the index index, and the length is an    int compare (size_ Type index, size_type length, const basic_string &STR, Size_type index2,    size_type length2);//Compare your own substrings and STR strings, where Index2 and length2 reference str,index and length refer to themselves    int compare (size_type index, size_type length, const char *STR, Size_type LENGTH2);//Compare their substrings with the substring of STR, where the substring of STR starts at index 0, the length is length2, and its substring starts with index, length



   return value condition 

less than 0 this < Str
0 This = = str
greater than 0 this > str

Delete (erase)
  
Iterator Erase (iterator POS);//Deletes the character pointed to by POS, returns an iterator to the next character    iterator erase (iterator start, iterator end);//delete from start to End of all characters, returns an iterator that points to the next position of the last character being deleted    basic_string &erase (size_type index = 0, size_type num = NPOs);//delete from index cable The starting NUM character, returned *this


Inserting (insert)
  
Iterator Insert (iterator I, const char &ch);//Insert a character in front of the position represented by the iterator I ch    basic_string &insert (size_type index, C Onst basic_string &str);//position of the string at index Insert string str    basic_string &insert (size_type index, const char *STR);// In the position of the string index Insert string str    basic_string &insert (size_type index1, const basic_string &STR, Size_type index2, Size_type num)///In the position of the string index inserts a substring of STR (starting from INDEX2, long num characters)    basic_string &insert (size_type index, const Char *str, size_type num);//The position of the string at index Insert string str of num characters    basic_string &insert (size_type index, Size_type num, char ch);//The position of the string index inserts the copy of the NUM character ch    (iterator I, size_type num, const char &ch);//Inserts the NU in front of the position represented by the iterator Copy of M-character ch    void Insert (iterator I, iterator start, iterator end);//Insert a character before the position represented by the iterator I, starting with start, ending with end




Replacement (replace)
  
 basic_string &replace (size_type index, size_type num, const basic_string &AMP;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 &AMP;STR, Size_type index2, Size_type num2);//replace the characters in this string with num2 characters in Str (starting with INDEX2), starting with index1, up to NUM1 characters basic_string &replace (size_type index , Size_type num, const char *STR);//replace 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);//replace the characters in this string with num2 characters in Str (starting with INDEX2), starting with index1, NUM1 characters basic_string &r  Eplace (size_type index, Size_type num1, size_type num2, char ch);//Replace 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);//replace 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);//Replace the contents of this string with STR, and the iterator start and end indicates the range basic_string &am P;replace (iterator start, IteraTor end, const char *STR, Size_type num),//Replace the contents of this string with num characters in Str, and the iterator start and end indicates the range basic_string &replace (iterator Start, iterator end, size_type num, char ch);//Replace the contents of this string with num CH characters, and the iterator start and end indicates the range



Find
The following various find functions are very similar, so there is no one by one comment
return value: Returns String::npos if not found

Find
   
Size_type Find (const basic_string &STR, size_type index);//returns the position where Str first appears in the string (from index)    Size_type Find (  const char *STR, size_type index);//returns the position where Str first appears in the string (from index)    size_type find (const char *STR, SIZE_TYPE index, Size_type length);//returns the position where Str first appears in the string (from index, length)    size_type find (char ch, size_type index);// Returns the position of the first occurrence of the character ch in the string (find from index)



Find_first_of: Finds the first character in a string that matches one of the characters 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: Finds the first character in a string that does not match the characters 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: Finds the last character in a string that matches one of the characters 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: Finds the last character in a string that does not match the characters 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 and finds it from index
 
Size_type rfind (const char *STR, size_type index);//Returns the last character that matches a character in Str, looking for  size_type rfind from index (const char *s TR, size_type index, Size_type num);//Returns the last character that matches a character in Str, finds from index, finds up to Num characters  size_type rfind (char ch, size_ Type index);//Returns the last character that matches ch, looking from index



at function
  
Reference at (size_type index); the//at () function returns a reference to the character at the index position. If index is not in the string range, at () will report an "out of range" error and throw a Out_of_range exception


Begin function
 
Iterator begin (); the//begin () function returns an iterator that points to the first element of the string


End Function
 
Iterator end ();//Returns an iterator that points to the end of the string (the next position of the last character)


C_str function
   
const char *C_STR ();//Returns a pointer to a regular C string with the same contents as this string


Capacity function
 
  Size_type capacity ();//Returns the number of characters the string can hold before re-applying more space. This number is at least as large as size ()


copy function
  
Size_type copy (char *str, size_type num, size_type index);//copy your own num characters into STR (starting at index indices). The return value is the number of characters copied


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 false (false)


Get_allocator function
  
Allocator_type get_allocator ();//Configurator that returns this string



Length function
  
Size_type length ();//Returns the lengths of the strings. 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 hold


Rbegin function
   
Rbegin ();//Returns a reverse iterator pointing to the last character



rend function
   
Rend ();//Returns a reverse iterator that points to the previous position of the first element


Reserve function
 
  


Resize function
 
void resize (size_type num);//Change the size of this string to num, the contents of the new space are indeterminate  void resize (size_type num, char ch);//You can also specify to fill with CH


Size function
   
Size ();//Returns the number of characters in a string


substr function
    
basic_string substr (size_type index, size_type num = NPOs);//Returns a substring of this string, starting with index, with a length of num characters. If not specified, it will be the default value of String::npos. This way, the substr () function will simply return the remaining string starting at index



Swap function

void swap (basic_string &str);//Exchange STR and this string




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

C + + String type details

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.