The beauty of detailed explanation of string usage in C + +

Article directory

Constructors
Operators
Add text (append)
Assignment
        at
        begin
        c_str
Capacity
Compare
Copy
        data
        empty
        end
Delete (erase)
Find
        find_first_of
        find_first_not_of
        find_last_of
        find_last_not_of
        get_allocator
Insert (insert)
Length
        max_size
        rbegin
        rend
Replace (replace)
Reserve
        resize
        rfind
        size
        substr
Swap (swap)

Constructors

Syntax:

string();   
string( size_type length, char ch );   
string( const char *str );   
string( const char *str, size_type length );   
string( string &str, size_type index, size_type length );   
string( input_iterator start, input_iterator end ); 

The constructor of a string creates a new string, including:

  • Copy of ch with length (i.e. length ch)
  • Start with str (any length),
  • The substring starting with index, with length, or
  • Take the element from start to end as the initial value

For example,

    string str1( 5, 'c' );
    string str2( "Now is the time..." );
    string str3( str2, 11, 4 );
    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;

display

    ccccc
    Now is the time...
    time 

Operators

Syntax:

`  ==   >   <   >=   <=   !=   +   +=   [] `

You can use = =, >, <, > =, < =, and! = to compare strings. You can use the + or + = operators to connect two strings, and you can use [] to get specific characters

Add text (append)

Syntax:

basic_string &append( const basic_string &str );   
basic_string &append( const char *str );  
basic_string &append( const basic_string &str, size_type index, size_type len );   
basic_string &append( const char *str, size_type num );   
basic_string &append( size_type num, char ch );   
basic_string &append( input_iterator start, input_iterator end );

The append() function does the following:

  • Add str at the end of the string,
  • Add a substring of str at the end of the string. The substring starts with index index index and is len long
  • Add num characters in str at the end of the string,
  • Add num characters ch at the end of the string,
  • Add a sequence of characters at the end of the string in the form of iterators start and end

For example, the following code:

    string str = "Hello World";
    str.append( 10, '!' );
    cout << str << endl;

display

    Hello World!!!!!!!!!!

Assignment

Syntax:

basic_string &assign( const basic_string &str );   
basic_string &assign( const char *str );   
basic_string &assign( const char *str, size_type num );   
basic_string &assign( const basic_string &str, size_type index, size_type len );   
basic_string &assign( size_type num, char ch );

The function is assigned in the following ways:

  • str is used to assign a value to a string,
  • Value the string with the start num characters of str,
  • The substring of str is used to assign a value to the string. The substring starts with index index, and the length is len
  • Assign a value to a string with the num characters ch

For example, the following code:

    string str1, str2 = "War and Peace";
    str1.assign( str2, 4, 3 );  
    cout << str1 << endl;

display

    and

at

Syntax:

`  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() will report an "out of range" error and throw an out of range exception. For example, the following code:

    string text = "ABCDEF";
    char ch = text.at( 2 );

Display character 'C'

begin

Syntax:

`  iterator begin(); `

The begin() function returns a iterator , points to the first element of the string

Related topics:
end()

c_str

Syntax:

`  const char *c_str(); `

The C ﹣ str() function returns a pointer to a regular C string with the same contents as this string

Capacity

Syntax:

`  size_type capacity(); `

The capacity() function returns the number of characters the string can hold before re applying for more space. This number is at least the same as size() The same big as that.

Related topics:
max_size(), reserve(), resize(), size()

Compare

Syntax:

int compare( const basic_string &str );   
int compare( const char *str );   
int compare( size_type index, size_type length, const basic_string &str );   int compare( size_type index, size_type length, const basic_string &str, size_type index2,   size_type length2 );   
int compare( size_type index, size_type length, const char *str, size_type length2 ); 

The compare() function compares this string with str in several ways, and returns:

Return value Situation
Less than zero this < str
Zero this == str
Greater than zero this > str

Different functions:

Compare yourself to str,
Compare your own substring with str. the substring starts with index index, and the length is length
Compare your own substring with that of str, where index2 and length2 refer to str, and index and length refer to themselves
Compare your own substring and str's substring, where str's substring starts with index 0, with length of length2, and your own substring starts with index, with length of

Copy (copy)

Syntax:

`  size_type copy( char *str, size_type num, size_type index ); `

The copy() function copies its num characters into str (starting with index). The return value is the number of characters copied

data

Syntax:

`  const char *data(); `

The data() function returns a pointer to its first character

Related topics:
c_str()

empty

Syntax:

`  bool empty(); `

empty() returns true if the string is empty, false otherwise

end

Syntax:

`  iterator end(); `

The end() function returns a iterator , points to the end of the string (the next position of the last character)

Related topics:
begin()

Delete (erase)

Syntax:

iterator erase( iterator pos );   
iterator erase( iterator start, iterator end );   
basic_string &erase( size_type index = 0, size_type num = npos ); 

The erase() function can:

Delete the character pointed to by pos, and return the iterator pointing to the next character,
Delete all characters from start to end, return an iterator, pointing to the next position of the last character deleted
Delete the num characters from the index index and return * this

The parameters index and num have default values, which means erase() can be called as follows: only index is used to delete all characters after index, or no parameter is used to delete all characters. For example:
 

string s("So, you like donuts, eh? Well, have all the donuts in the world!");
cout << "The original string is '" << s << "'" << endl;
  
s.erase( 50, 14 );
cout << "Now the string is '" << s << "'" << endl;

s.erase( 24 );
cout << "Now the string is '" << s << "'" << endl;

s.erase();
cout << "Now the string is '" << s << "'" << endl;

Will show

    The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!'
    Now the string is 'So, you like donuts, eh? Well, have all the donuts'
    Now the string is 'So, you like donuts, eh?'
    Now the string is ''

Find (find)

Syntax:

size_type find( const basic_string &str, size_type index );   
size_type find( const char *str, size_type index );   
size_type find( const char *str, size_type index, size_type length );   size_type find( char ch, size_type index ); 

find() function:

  • Returns the first occurrence of str in the string (starting with index) *. *If not found, return string::npos,
  • Returns the first occurrence of str in the string (starting from index, with length). If not, return string::npos,
  • Returns the first occurrence of the character ch in the string, starting with index. If not found, return string::npos

For example,
 

    string str1( "Alpha Beta Gamma Delta" );
    unsigned int loc = str1.find( "Omega", 0 );
    if( loc != string::npos )
      cout << "Found Omega at " << loc << endl;
    else
      cout << "Didn't find Omega" << endl;

find_first_of

Syntax:

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? Of() function:

  • Finds the first character in the string that matches a character in str and returns its location. The search starts from index and returns string::npos if it is not found
  • Finds the first character in the string that matches a character in str and returns its location. Search starts with index, with a maximum of num characters. If not, return string::npos,
  • Finds the first character in the string that matches the ch and returns its position. Search starts with index.

Related topics:
find()


find_first_not_of

Syntax:
 

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 first not of() function:

  • Find the first character in the string that doesn't match any of the characters in str and return its position. Search starts with index. string::nops if not found
  • Find the first character in the string that doesn't match any of the characters in str and return its position. The search starts with index and looks up to num characters. string::nops if not found
  • Finds the first character in the string that does not match the ch and returns its position. Search starts with index. string::nops if not found

Related topics:

find()

find_last_of

Syntax:

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 ﹐ of() function:

  • Finds the last character in the string that matches a character in str and returns its location. Search starts with index. string::nops if not found
  • Finds the last character in the string that matches a character in str and returns its location. Search starts with index, with a maximum of num characters. string::nops if not found
  • Finds the last character in the string that matches the ch and returns its position. Search starts with index. string::nops if not found

Related topics:

find()

find_last_not_of

Syntax:

 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 ); 

Find ﹐ last ﹐ not ﹐ of() function:

  • Find the last character in the string that doesn't match the character in str and return its position. Search starts with index. string::nops if not found
  • Find the last character in the string that doesn't match the character in str and return its position. The search starts from index. If the maximum number of characters is num, string::nops will be returned
  • Finds the last character in the string that does not match the ch and returns its position. Search starts with index. string::nops if not found

Related topics:
find()

get_allocator

Syntax:

`  allocator_type get_allocator(); `

The get allocator() function returns the configurator of this string

Insert (insert)

Syntax:

iterator insert( iterator i, const char &ch );   
basic_string &insert( size_type index, const basic_string &str ); 
basic_string &insert( size_type index, const char *str );   
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );   
basic_string &insert( size_type index, const char *str, size_type num );   
basic_string &insert( size_type index, size_type num, char ch );   
void insert( iterator i, size_type num, const char &ch );  
void insert( iterator i, iterator start, iterator end ); 

The insert() function has many functions:

  • Insert a character ch before the position represented by iterator i,
  • Insert the string str in the position index of the string,
  • Insert the substring of string str in the position index of string (starting from index2, num characters long),
  • Insert num characters of string str in the position index of string,
  • Insert a copy of the num character ch in the position index of the string,
  • Insert a copy of the num character ch before the position represented by the iterator i,
  • Insert a character before the position represented by iterator i, starting with start and ending with end

Related topics:

replace()

Length

Syntax:

size_type length(); 

The length() function returns the length of the string. This number should match the size() The number returned is the same

Related topics:
size()

max_size

Syntax:

`  size_type max_size(); `

The max_size() function returns the maximum number of characters a string can hold.

rbegin

Syntax:

`  const reverse_iterator rbegin(); `

rbegin() returns an inverse iterator , points to the last character of the string.

Related topics:
rend()

rend

Syntax:

`  const reverse_iterator rend(); `

The rend() function returns an inverse iterator , points to the beginning of the string (the position preceding the first character).

Related topics:
rbegin()

Replace (replace)

Syntax:

 basic_string &replace( size_type index, size_type num, const basic_string &str );   
 basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,   size_type num2 );   
 basic_string &replace( size_type index, size_type num, const char *str );   
 basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );   
 basic_string &replace( size_type index, size_type num1, size_type num2, char ch );   
 basic_string &replace( iterator start, iterator end, const basic_string &str );   
 basic_string &replace( iterator start, iterator end, const char *str ); 
 basic_string &replace( iterator start, iterator end, const char *str, size_type num );   
 basic_string &replace( iterator start, iterator end, size_type num, char ch ); 

replace() function:

  • Replace the characters in this string with num characters in str, starting with index
  • Replace the characters in this string with num2 characters in str (starting from index2), starting from index1 and at most num1 characters
  • Replace the characters in this string with num characters in str (starting from index)
  • Replace the characters in this string with num2 characters in str (starting from index2), starting from index1 and num1 characters
  • Replace the characters in this string with num2 ch characters, starting with index
  • Replace the characters in this string with the characters in str, and the iterator start and end indicate the range
  • Replace the contents of this string with num characters in str, and the iterator start and end indicate the range,
  • Replace the contents of this string with num ch characters, and the range indicated by the iterator start and end

For example, the following code displays the string "they say he carried it himself find your soul-mate, Homer."
 

    string s = "They say he carved it himself...from a BIGGER spoon";
    string s2 = "find your soul-mate, Homer.";

    s.replace( 32, s2.length(), s2 );

    cout << s << endl;

Related topics:
insert()

Reserve

Syntax:

`  void reserve( size_type num ); `

The reserve() function sets the capacity To preserve num character space.

Related topics:
capacity()

resize

Syntax:

void resize( size_type num );  
void resize( size_type num, char ch ); 

The resize() function changes the size of this string to num, and the content of the new space is uncertain. You can also specify to fill with ch.

rfind

Syntax:

size_type rfind( const basic_string &str, size_type index );   
size_type rfind( const char *str, size_type index );   
size_type rfind( const char *str, size_type index, size_type num );   
size_type rfind( char ch, size_type index ); 

rfind() function:

  • Returns the last character that matches a character in str, starting with index. If not found, return string::npos
  • Returns the last character matching a character in str, starting from index, and searching for num characters at most. If not found, return string::npos
  • Returns the last character that matches the ch, starting with index. If not found, return string::npos

For example, the first call to rfind() in the following code returns string::npos because the target word is not in the first eight characters. However, the second call returns 9 because the target word is in the first 20 characters.
 

    int loc;
    string s = "My cat's breath smells like cat food.";

    loc = s.rfind( "breath", 8 );
    cout << "The word breath is at index " << loc << endl;

    loc = s.rfind( "breath", 20 );
    cout << "The word breath is at index " << loc << endl; 

Related topics:
find()

size

Syntax:

`  size_type size(); `

The size() function returns the number of characters now in the string.

Related topics:
length(), max_size()

substr

Syntax:

`  basic_string substr( size_type index, size_type num = npos ); `

substr() returns a substring of this string, which is num characters long starting from index. If not specified, the default value is string::npos. In this way, the substr () function simply returns the rest of the string starting with index.

For example:

    string s("What we have here is a failure to communicate");

    string sub = s.substr(21);

    cout << "The original string is " << s << endl;
    cout << "The substring is " << sub << endl;

Show:

    The original string is What we have here is a failure to communicate
    The substring is a failure to communicate

Exchange (swap)

Syntax:

`  void swap( basic_string &str ); `

The swap() function exchanges str with this string. For example:

    string first( "This comes first" );
    string second( "And this is second" );
    first.swap( second );
    cout << first << endl;
    cout << second << endl;

Show:

    And this is second
    This comes first

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

74 original articles published, 148 praised, 10000 visited+
Private letter follow

Tags: less REST

Posted on Sat, 08 Feb 2020 06:45:48 -0500 by sabatier