C++ Reference ยท Strings and I/O
C++ stoi, stod and to_string
Convert between numbers and std::string with stoi, stol, stod, to_string: bases, the pos output, invalid_argument and out_of_range errors, and better options.
The functions std::stoi, stol, stoll, stoul, stoull, stof, stod and stold turn text into numbers. std::to_string goes the other way. All of them live in <string> and were added in C++11.
The sto* functions skip leading whitespace, read as many characters as form a valid number, and stop at the first character that does not fit. They do not require the whole string to be a number: std::stoi("42abc") returns 42. They report problems by throwing exceptions, which makes them convenient for quick code but clumsy when bad input is normal. For that case, see to_chars and from_chars.
int n = std::stoi("123"); // 123 double d = std::stod("3.5e2"); // 350.0 std::string s = std::to_string(42); // "42"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
int stoi (const std::string& str, std::size_t* pos = nullptr, int base = 10); long stol (const std::string& str, std::size_t* pos = nullptr, int base = 10); long long stoll(const std::string& str, std::size_t* pos = nullptr, int base = 10); unsigned long stoul (const std::string& str, std::size_t* pos = nullptr, int base = 10); unsigned long long stoull(const std::string& str, std::size_t* pos = nullptr, int base = 10); float stof (const std::string& str, std::size_t* pos = nullptr); double stod (const std::string& str, std::size_t* pos = nullptr); long double stold(const std::string& str, std::size_t* pos = nullptr); std::string to_string(int value); // and long, long long, unsigned..., float, double, long double
| Name | Description |
|---|---|
str |
The text to parse (a std::wstring overload set exists too). |
pos |
If not null, receives the index of the first unconverted character. |
base |
2 to 36, or 0 to auto-detect (0x hex, leading 0 octal, otherwise decimal). Integer functions only. |
value |
The number to format. |
Returns: the parsed number, or the formatted string.
Errors: std::invalid_argument if no conversion could be made (no digits at the start); std::out_of_range if the value does not fit the type. stoul("-1") does not throw: it wraps to the maximum value.
Usage notes
- Partial parsing is silent.
stoi("12abc")is 12. Pass&posand compare it withsize()when the whole input must be a number. - Cost of exceptions. Input validation by
try/catchis slow when many inputs are bad; usefrom_charsorstrtolthere. to_stringof floating-point uses the%fformat: six digits after the point, no exponent, no round-trip guarantee. For control use std::format ("{:.2f}") orto_chars.- Locale.
stodandto_string(double)use the current C locale for the decimal point; the default "C" locale means.. charis not special.std::to_string('A')converts the integer 65 to"65".- Hex and binary output are not supported by
to_string; usestd::format("{:x}", n)orstd::bitset. - Whitespace and signs. A leading
+or-is accepted; leading whitespace is skipped; trailing whitespace is left unconverted. stoireturnsintand throws for values outside its range even iflongwould hold them.