Class template basic_string
provides typical string-manipulation operations.
The typedef
statement
typedef basic_string< char > string;
creates the alias type string
for basic_string<char>
(p. 870). A typedef
also is provided for the wchar_t
type (wstring
).
To use string
s, include C++ Standard Library header <string>
.
Assigning a single character to a string
object is permitted in an assignment statement.
string
s are not necessarily null terminated.
Most string
member functions take as arguments a starting subscript location and the number of characters on which to operate.
string
member functions size
and length
(p. 871
) return the number of characters currently stored in a string
.
string
Assignment and ConcatenationClass string
provides overloaded operator=
and function assign
(p. 871
) for assignments.
The subscript operator, []
, provides read/write access to any element of a string
.
string
member function at
(p. 873) provides checked access (p. 873)—going past either end of the string
throws an out_of_range
exception. The subscript operator, []
, does not provide checked access.
The overloaded +
and +=
operators and member function append
(p. 873) perform string
concatenation.
string
sClass string
provides overloaded ==
, !=
, <
, >
, <=
and >=
operators for string
comparisons.
string
member function compare
(p. 875
) compares two string
s (or substrings) and returns 0
if the string
s are equal, a positive number if the first string
is lexicographically (p. 875) greater than the second or a negative number if the first string is lexicographically less than the second.
string
member function substr
(p. 876
) retrieves a substring from a string
.
string
sstring
member function swap
(p. 876
) swaps the contents of two string
s.
string
Characteristicsstring
member function capacity
(p. 879
) returns the total number of characters that can be stored in a string
without increasing the amount of memory allocated to the string
.
string
member function max_size
(p. 879
) returns the maximum size a string
can have.
string
member function resize
(p. 879
) changes the length of a string
.
string
member function empty
returns true
if a string
is empty.
string
Class string
find functions (p. 881
) find
, rfind
, find_first_of
, find_last_of
and find_first_not_of
locate substrings or characters in a string
.
string
string
string
member function insert
(p. 884
) inserts characters in a string
.
char
* Stringsstring
member function c_str
(p. 885
) returns a const char*
pointing to a null-terminated pointer-based string that contains all the characters in a string
.
string
member function data
(p. 885) returns a const char*
pointing to a non-null-terminated built-in character array that contains all the characters in a string
.
C++11’s <string>
header now contains functions for converting from numeric values to string objects and from string objects to numeric values.
The to_string
function (p. 890
) returns the string representation of its numeric argument and is overloaded for types int
, unsigned int
, long
, unsigned long
, long long
, unsigned long long
, float
, double
and long double
.
C++11 provides eight functions for converting string objects to numeric values. Each function attempts to convert the beginning of its string argument to a numeric value. If no conversion can be performed, an invalid_argument
exception occurs. If the result of the convertion is out of range for the function’s return type, an out_of_range
exception occurs.
Each function that converts a string
to an integral type receives three parameters—a string
containing the characters to convert, a pointer to a size_t
variable where the function stores the index of the first character that was not converted (a null pointer, by default) and an int
from 2 to 36 representing the number’s base (base 10, by default).
The functions that convert strings to floating-point types each receive two parameters—a string
containing the characters to convert and a pointer to a size_t
variable where the function stores the index of the first character that was not converted (a null pointer, by default).