std::string()
thiendepzai1092006 / c string
c string
A comprehensive cheat sheet covering essential methods of the std::string class in C++, with practical examples and best practices for efficient string manipulation.
Constructors, Assignment & Capacity
Constructors
|
Default constructor. Creates an empty string.
|
|
Copy constructor. Creates a string as a copy of another string.
|
|
Constructs a string from a C-style string.
|
|
Constructs a string from the first
|
|
Constructs a string containing
|
|
Constructs a string from a range specified by iterators.
|
Assignment
|
Copy assignment. Assigns the contents of
|
|
Assigns a C-style string to
|
|
Assigns a single character to
|
|
Assigns the content of
|
|
Assigns the first
|
Capacity
|
Returns the number of characters in the string.
|
|
Returns the maximum possible size of the string, based on system limitations.
|
|
Returns the number of characters the string has allocated space for. May be greater than
|
|
Reserves space for at least
|
|
Reduces the string’s capacity to match its size, freeing up unused memory.
|
|
Returns
|
Element Access & Modifiers
Element Access
|
Accesses the character at
|
|
Accesses the character at
|
|
Returns a reference to the first character.
|
|
Returns a reference to the last character.
|
Modifiers
|
Appends
|
|
Appends the first
|
|
Inserts
|
|
Erases
|
|
Replaces
|
|
Removes all characters from the string, making it empty.
|
|
Appends the character
|
|
Removes the last character from the string. Undefined behavior if the string is empty.
|
|
Resizes the string to length
|
String Operations & C-Style Conversion
String Searching
|
Finds the first occurrence of substring
|
|
Finds the last occurrence of substring
|
|
Finds the first occurrence of any character from
|
|
Finds the last occurrence of any character from
|
|
Finds the first character in
|
|
Finds the last character in
|
String Operations
|
Returns a new string containing a substring of
|
|
Compares
|
|
Compares a substring of
|
C-Style String Conversion
|
Returns a pointer to a null-terminated C-style string representing the contents of
|
|
Returns a pointer to the underlying character array of the string. The returned pointer is not guaranteed to be null-terminated before C++17. From C++17 onward, it is guaranteed to be null-terminated, but modifying the string in any way invalidates the pointer. Use with caution.
|
|
A non-owning view of a string-like object (including
|
Iterators and Non-Member Functions
Iterators
|
Returns an iterator to the beginning of the string.
|
|
Returns an iterator to the end of the string.
|
|
Returns a reverse iterator to the beginning of the reversed string (i.e., the last character).
|
|
Returns a reverse iterator to the end of the reversed string (i.e., one position before the first character).
|
|
Return const iterators, preventing modification of the string’s characters through the iterator.
|
Non-Member Functions / Operators
|
String concatenation. Returns a new string that is the concatenation of
|
|
Concatenation with a C-style string.
|
|
Comparison operators. Perform lexicographical comparison between strings. Return
|
|
Reads a line from the input stream
|
Best Practices and Common Pitfalls
|