Strings in C++
Strings in C++
Introduction:
Strings in C++ are used to store text or sequence of characters. In C++
strings can be stored in one of the two following ways:
• C style string (using characters)
• String class
In the case of strings, memory is allocated The size of the character array has to
dynamically. More memory can be be allocated statically, more memory
allocated at run time on demand. As no cannot be allocated at run time if
memory is preallocated, no memory is required. Unused allocated memory is
wasted. also wasted
String Char Array
Operations on Strings
1) Input Functions
Function Definition
push_back() This function is used to input a character at the end of the string.
2) Capacity Functions
Function Definition
This function returns the capacity allocated to the string, which can
capacity()
be equal to or more than the size of the string. Additional space is
Function Definition
allocated so that when the new characters are added to the string,
the operations can be done efficiently.
This function changes the size of the string, the size can be
resize() increased or decreased.
This function decreases the capacity of the string and makes it equal
to the minimum capacity of the string. This operation is useful to
save additional memory if we are sure that no further addition of
shrink_to_fit() characters has to be made.
3) Iterator Functions
Function Definition
end() This function returns an iterator to the next to the end of the string.
rbegin() This function returns a reverse iterator pointing at the end of the string.
This function returns a constant iterator pointing to the next of end of the
cend() string, it cannot be used to modify the contents it points-to.
4) Manipulating Functions:
Function Definition