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

出自cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
在標頭 <string> 定義
int       stoi ( const std::string& str,
                 std::size_t* pos = nullptr, int base = 10 );
(1)(C++11 起)
int       stoi ( const std::wstring& str,
                 std::size_t* pos = nullptr, int base = 10 );
(2)(C++11 起)
long      stol ( const std::string& str,
                 std::size_t* pos = nullptr, int base = 10 );
(3)(C++11 起)
long      stol ( const std::wstring& str,
                 std::size_t* pos = nullptr, int base = 10 );
(4)(C++11 起)
long long stoll( const std::string& str,
                 std::size_t* pos = nullptr, int base = 10 );
(5)(C++11 起)
long long stoll( const std::wstring& str,
                 std::size_t* pos = nullptr, int base = 10 );
(6)(C++11 起)

轉譯字元串 str 中的有符號整數值。

ptr 為(提供給轉換函數的)一個類型是 char* (1,3,5)wchar_t* (2,4,6) 的內部指針:

1) 調用 std::strtol(str.c_str(), &ptr, base)
2) 調用 std::wcstol(str.c_str(), &ptr, base)
3) 調用 std::strtol(str.c_str(), &ptr, base)
4) 調用 std::wcstol(str.c_str(), &ptr, base)
5) 調用 std::strtoll(str.c_str(), &ptr, base)
6) 調用 std::wcstoll(str.c_str(), &ptr, base)

捨棄所有空白符(以調用 std::isspace 鑒別),直到找到首個非空白符,然後取儘可能多的字元組成合法的底 n (其中 n=base)的整數表示,並將它們轉換成一個整數。合法的整數由下列部分組成:

  • (可選)正或負號
  • (可選)指示八進制底的前綴(0)(僅當底為 80 時應用)
  • (可選)指示十六進制底的前綴(0x0X)(僅當底為 160 時應用)
  • 數字序列

底的合法集是 {0, 2, 3, ..., 36}。合法數字集對於底 2 整數是 {0, 1},對於底 3 整數是 {0, 1, 2},以此類推。對於大於 10 的底,合法數字包含字母字元,從對於底 11 整數的 Aa 到對於底 36 整數的 Zz。忽略字元大小寫。

當前安裝的 C 本地環境可能接受另外的數字格式。

如果 base0,那麼自動檢測數值進制:如果前綴是 0,那麼底是八進制,如果前綴是 0x0X,那麼底是十六進制,否則底是十進制。

如果符號是輸入序列的一部分,那麼對從數字序列計算得來的數字值取反,如同用結果類型的一元減

如果 pos 不是空指針,那麼指針 ptr 會接收 str.c_str() 中首個未轉換字元的地址,將計算該字元的下標將它存儲到 *pos,該對象會給出轉換所處理的字元數。

目錄

[編輯] 參數

str-要轉換的字元串
pos-存儲已處理字元數的整數的地址
base-數的底

[編輯] 返回值

對應 str 內容的整數值。

[編輯] 異常

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <utility>
 
int main()
{
    const auto data =
    {
        "45",
        "+45",
        " -45",
        "3.14159",
        "31337 with words",
        "words and 2",
        "12345678901",
    };
 
    for (const std::string s : data)
    {
        std::size_t pos{};
        try
        {
            std::cout << "std::stoi(" << std::quoted(s) << "):";
            const int i{std::stoi(s, &pos)};
            std::cout << i << ";pos:" << pos << '\n';
        }
        catch (std::invalid_argument const& ex)
        {
            std::cout << "std::invalid_argument::what():" << ex.what() << '\n';
        }
        catch (std::out_of_range const& ex)
        {
            std::cout << "std::out_of_range::what():" << ex.what() << '\n';
            const long long ll{std::stoll(s, &pos)};
            std::cout << "std::stoll(" << std::quoted(s) << "):" << ll
                      << ";pos:" << pos << '\n';
        }
    }
 
    std::cout << "\n以不同的底进行调用:\n";
    for (const auto& [s, base] : {std::pair<const char*, int>
        {"11",  2}, {"22",  3}, {"33",  4}, {"77",  8},
        {"99", 10}, {"FF", 16}, {"jJ", 20}, {"Zz", 36}})
    {
        const int i{std::stoi(s, nullptr, base)};
        std::cout << "std::stoi(" << std::quoted(s)
                  << ", nullptr, " << base << "):" << i << '\n';
    }
}

可能的輸出:

std::stoi("45"):45;pos:2
std::stoi("+45"):45;pos:3
std::stoi(" -45"):-45;pos:4
std::stoi("3.14159"):3;pos:1
std::stoi("31337 with words"):31337;pos:5
std::stoi("words and 2"):std::invalid_argument::what():stoi
std::stoi("12345678901"):std::out_of_range::what():stoi
std::stoll("12345678901"):12345678901;pos:11
 
以不同的底进行调用:
std::stoi("11", nullptr, 2):3
std::stoi("22", nullptr, 3):8
std::stoi("33", nullptr, 4):15
std::stoi("77", nullptr, 8):63
std::stoi("99", nullptr, 10):99
std::stoi("FF", nullptr, 16):255
std::stoi("jJ", nullptr, 20):399
std::stoi("Zz", nullptr, 36):1295

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告應用於出版時的行為正確行為
LWG 2009C++11std::strtolstd::strtollerrno 設置為 ERANGE 時不會拋出 std::out_of_range會拋出

[編輯] 參閱

(C++11)(C++11)
轉換字元串為無符號整數
(函數) [編輯]
(C++11)(C++11)(C++11)
轉換字元串為浮點數
(函數) [編輯]
轉換位元組字元串為整數值
(函數) [編輯]
轉換位元組字元串為無符號整數值
(函數) [編輯]
(C++11)(C++11)
轉換位元組字元串為 std::intmax_tstd::uintmax_t
(函數) [編輯]
(C++17)
轉換字元序列到整數或浮點數
(函數) [編輯]
轉換位元組字元串為整數值
(函數) [編輯]
(C++11)
轉換整數或浮點數為 string
(函數) [編輯]
(C++11)
轉換整數或浮點數為 wstring
(函數) [編輯]