|
|
|
Defined in header <string> | ||
int stoi(conststd::string& str, std::size_t* pos= nullptr,int base=10); | (1) | (since C++11) |
int stoi(conststd::wstring& str, std::size_t* pos= nullptr,int base=10); | (2) | (since C++11) |
long stol(conststd::string& str, std::size_t* pos= nullptr,int base=10); | (3) | (since C++11) |
long stol(conststd::wstring& str, std::size_t* pos= nullptr,int base=10); | (4) | (since C++11) |
longlong stoll(conststd::string& str, std::size_t* pos= nullptr,int base=10); | (5) | (since C++11) |
longlong stoll(conststd::wstring& str, std::size_t* pos= nullptr,int base=10); | (6) | (since C++11) |
Interprets a signed integer value in the stringstr.
Letptr be an internal (to the conversion functions) pointer of typechar*(1,3,5) orwchar_t*(2,4,6), accordingly.
Discards any whitespace characters (as identified by callingstd::isspace) until the first non-whitespace character is found, then takes as many characters as possible to form a validbase-n (where n=base
) integer number representation and converts them to an integer value. The valid integer value consists of the following parts:
0
) indicating octal base (applies only when the base is8 or0)0x
or0X
) indicating hexadecimal base (applies only when the base is16 or0)The set of valid values for base is{0, 2, 3, ..., 36}
. The set of valid digits for base-2
integers is{0, 1}
, for base-3
integers is{0, 1, 2}
, and so on. For bases larger than10
, valid digits include alphabetic characters, starting fromAa
for base-11
integer, toZz
for base-36
integer. The case of the characters is ignored.
Additional numeric formats may be accepted by the currently installed Clocale.
If the value ofbase
is0, the numeric base is auto-detected: if the prefix is0
, the base is octal, if the prefix is0x
or0X
, the base is hexadecimal, otherwise the base is decimal.
If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if byunary minus in the result type.
Ifpos is not a null pointer, thenptr will receive an address of the first unconverted character instr.c_str(), and the index of that character will be calculated and stored in*pos, giving the number of characters that were processed by the conversion.
Contents |
str | - | the string to convert |
pos | - | address of an integer to store the number of characters processed |
base | - | the number base |
Integer value corresponding to the content ofstr.
#include <iomanip>#include <iostream>#include <stdexcept>#include <string>#include <utility> int main(){constauto data={"45","+45"," -45","3.14159","31337 with words","words and 2","12345678901",}; for(conststd::string s: data){std::size_t pos{};try{std::cout<<"std::stoi("<<std::quoted(s)<<"): ";constint i{std::stoi(s,&pos)};std::cout<< i<<"; pos: "<< pos<<'\n';}catch(std::invalid_argumentconst& ex){std::cout<<"std::invalid_argument::what(): "<< ex.what()<<'\n';}catch(std::out_of_rangeconst& ex){std::cout<<"std::out_of_range::what(): "<< ex.what()<<'\n';constlonglong ll{std::stoll(s,&pos)};std::cout<<"std::stoll("<<std::quoted(s)<<"): "<< ll<<"; pos: "<< pos<<'\n';}} std::cout<<"\nCalling with different radixes:\n";for(constauto&[s, base]:{std::pair<constchar*,int>{"11",2},{"22",3},{"33",4},{"77",8},{"99",10},{"FF",16},{"jJ",20},{"Zz",36}}){constint i{std::stoi(s, nullptr, base)};std::cout<<"std::stoi("<<std::quoted(s)<<", nullptr, "<< base<<"): "<< i<<'\n';}}
Possible output:
std::stoi("45"): 45; pos: 2std::stoi("+45"): 45; pos: 3std::stoi(" -45"): -45; pos: 4std::stoi("3.14159"): 3; pos: 1std::stoi("31337 with words"): 31337; pos: 5std::stoi("words and 2"): std::invalid_argument::what(): stoistd::stoi("12345678901"): std::out_of_range::what(): stoistd::stoll("12345678901"): 12345678901; pos: 11 Calling with different radixes:std::stoi("11", nullptr, 2): 3std::stoi("22", nullptr, 3): 8std::stoi("33", nullptr, 4): 15std::stoi("77", nullptr, 8): 63std::stoi("99", nullptr, 10): 99std::stoi("FF", nullptr, 16): 255std::stoi("jJ", nullptr, 20): 399std::stoi("Zz", nullptr, 36): 1295
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2009 | C++11 | std::out_of_range would not be thrown if std::strtol orstd::strtoll setserrno toERANGE | will throw |
(C++11)(C++11) | converts a string to an unsigned integer (function)[edit] |
(C++11)(C++11)(C++11) | converts a string to a floating point value (function)[edit] |
(C++11) | converts a byte string to an integer value (function)[edit] |
(C++11) | converts a byte string to an unsigned integer value (function)[edit] |
(C++11)(C++11) | converts a byte string tostd::intmax_t orstd::uintmax_t (function)[edit] |
(C++17) | converts a character sequence to an integer or floating-point value (function)[edit] |
(C++11) | converts a byte string to an integer value (function)[edit] |
(C++11) | converts an integral or floating-point value tostring (function)[edit] |
(C++11) | converts an integral or floating-point value towstring (function)[edit] |