[ Web Proxy ]
URL:
Viewing: http://ja.cppreference.com/cpp/string/basic_string/stol [Back]  [Original]

std::stoi, std::stol, std::stoll - cppreference.com
cppreference.com

std::stoi, std::stol, std::stoll

: cppreference.com
 
C++
(C++20)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
 
 
std::basic_string
 
<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 ( 8 0 )
  • () 16 0x 0X ( 16 0 )

{0,2,3,...,36} 2 {0,1} 3 {0,1,2} 10 11 Aa 36 Zz

C

base 0 0 8 0x 0X 1610

pos ptr () str.c_str() *pos

str -
pos -
base -

str

#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)

() [edit]
(C++11)(C++11)(C++11)

() [edit]

() [edit]

() [edit]
(C++11)(C++11)
std::intmax_t std::uintmax_t
() [edit]

() [edit]

() [edit]
(C++11)
string
() [edit]
wstring
() [edit]

Web Proxy Viewer  |  New URL  |  Original Page