std::string()
thiendepzai1092006 / c string
c string
A comprehensive guide to the std::string class in C++, covering constructors, modifiers, search functions, and best practices for efficient string manipulation.
Construction and Assignment
Constructors
|
Default constructor. Creates an empty string.
|
|
Copy constructor. Creates a string as a copy of another.
|
|
Constructs from a C-style string.
|
|
Constructs a substring from another string.
|
|
Constructs a string with
|
|
Constructs from a range specified by iterators.
|
Assignment Operators
|
Assigns a new value to the string. Supports assignment from another
|
|
Move assigns a new value to the string.
|
Capacity and Size
Size and Length
|
Returns the number of characters in the string.
|
|
Returns the maximum possible number of characters a
|
|
Returns
|
Capacity Management
|
Returns the number of characters the string has allocated space for. This can be greater than
|
|
Reserves storage for at least
|
|
Reduces the string’s capacity to match its size, releasing any excess memory. This is a non-binding request, and the implementation is free to ignore it.
|
Element Access
Character Access
|
Provides direct access to the character at the specified index. No bounds checking is performed. Undefined behavior if the index is out of range.
|
|
Provides access to the character at the specified index with bounds checking. Throws
|
|
Returns a reference to the first character of the string. Undefined behavior if the string is empty.
|
|
Returns a reference to the last character of the string. Undefined behavior if the string is empty.
|
String Operations and Search
Finding Substrings
|
Finds the first occurrence of the substring
|
|
Finds the last occurrence of the substring
|
|
Finds the first occurrence of any character from
|
|
Finds the last occurrence of any character from
|
|
Finds the first character that is not in
|
|
Finds the last character that is not in
|
Substring and Comparison
|
Returns a new string containing a substring of the original string, starting at position
|
|
Compares the string to another string lexicographically. Returns:
|