C++11 added functions for converting from numeric values to string
s and from string
s to numeric values. Though you could previously perform such conversions using other techniques, the functions presented in this section were added for convenience.
string
ObjectsC++11’s to_string
function (from the <string>
header) returns the string
representation of its numeric argument. The function is overloaded for types int
, unsigned int
, long
, unsigned long
, long long
, unsigned long long
, float
, double
and long double
.
string
Objects to Numeric ValuesC++11 provides eight functions (Fig. 21.13; from the <string>
header) 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, each function throws an invalid_argument
exception. If the result of the conversion is out of range for the function’s return type, each function throws an out_of_range
exception.
string
s to numeric types.Function | Return type |
---|---|
Functions that convert to integral types | |
stoi |
int |
stol |
long |
stoul |
unsigned long |
stoll |
long long |
stoull |
unsigned long long |
Functions that convert to floating-point types | |
stof |
float |
stod |
double |
stold |
long double |
string
s to Integral TypesConsider an example of converting a string
to an integral value. Assuming the string
:
string s("100hello");
the following statement converts the beginning of the string to the int
value 100
and stores that value in convertedInt
:
int convertedInt = stoi(s);
Each function that converts a string
to an integral type actually receives three parameters—the last two have default arguments. The parameters are:
A string
containing the characters to convert.
A pointer to a size_t
variable. The function uses this pointer to store the index of the first character that was not converted. The default argument is a null pointer, in which case the function does not store the index.
An int
from 2 to 36 representing the number’s base—the default is base 10.
So, the preceding statement is equivalent to
int convertedInt = stoi(s, nullptr, 10);
Given a size_t
variable named index
, the statement:
int convertedInt = stoi(s, &index, 2);
converts the binary number "100"
(base 2) to an int
(100 in binary is the int
value 4) and stores in index
the location of the string’s letter "h"
(the first character that was not converted).
string
s to Floating-Point TypesThe functions that convert string
s to floating-point types each receive two 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. The default argument is a null pointer, in which case the function does not store the index.
Consider an example of converting a string
to an floating-point value. Assuming the string
:
string s("123.45hello");
the following statement converts the beginning of the string
to the double
value 123.45
and stores that value in convertedDouble
:
double convertedDouble = stod(s);
Again, the second argument is a null pointer by default.