std::stoi, std::stol, std::stoll
: cppreference.com
<tbody>
</tbody>
int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 ); int stoi( const std::wstring& str, std::size_t* pos = 0, int base = 10 ); |
(1) | (C++11) |
long stol( const std::string& str, std::size_t* pos = 0, int base = 10 ); long stol( const std::wstring& str, std::size_t* pos = 0, int base = 10 ); |
(2) | (C++11) |
long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 ); long long stoll( const std::wstring& str, std::size_t* pos = 0, int base = 10 ); |
(3) | (C++11) |
str
1)
std::strtol(str.c_str(), &ptr, base) std::wcstol(str.c_str(), &ptr, base) 2)
std::strtol(str.c_str(), &ptr, base) std::wcstol(str.c_str(), &ptr, base) 3)
std::strtoll(str.c_str(), &ptr, base) std::wcstoll(str.c_str(), &ptr, base) ( isspace() ) base
- ()
- () 8
0(80) - () 16
0x0X(160)
{0,2,3,...,36} 2 {0,1} 3 {0,1,2} 10 11 Aa 36 Zz
base 0 0 8 0x 0X 1610
pos ptr () str.c_str() *pos
| str | - | |
| pos | - | |
| base | - |
str
- std::invalid_argument
- (std::strtol std::strtoll)
errnoERANGEstd::out_of_range
Run this code
#include <iostream>
#include <string>
int main()
{
std::string str1 = "45";
std::string str2 = "3.14159";
std::string str3 = "31337 with words";
std::string str4 = "words and 2";
int myint1 = std::stoi(str1);
int myint2 = std::stoi(str2);
int myint3 = std::stoi(str3);
// error: 'std::invalid_argument'
// int myint4 = std::stoi(str4);
std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
//std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
}
:
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
(C++11)(C++11) |
() |
(C++11)(C++11)(C++11) |
() |
(C++11) |
() |
(C++11) |
() |
(C++11)(C++11) |
std::intmax_t std::uintmax_t () |
(C++17) |
() |
(C++11) |
() |
(C++11) |
string () |
(C++11) |
wstring () |