std::stoi, std::stol, std::stoll
來自 cppreference.com
< cpp | string | 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 識別),直到找到第一個非空白字元,然後儘可能多地獲取字元以形成一個有效的 *base-n*(其中 n=base
)整數表示,並將其轉換為一個整數值。有效的整數值由以下部分組成:
- (可選) 加號或減號
- (可選) 字首 (
0
) 指示八進位制基數(僅當基數為 8 或 0 時適用) - (可選) 字首 (
0x
或0X
) 指示十六進位制基數(僅當基數為 16 或 0 時適用) - 一系列數字
基數的有效值集為 {0, 2, 3, ..., 36}
。基數 2
整數的有效數字集為 {0, 1}
,基數 3
整數的有效數字集為 {0, 1, 2}
,以此類推。對於大於 10
的基數,有效數字包括字母字元,從基數 11
整數的 Aa
到基數 36
整數的 Zz
。字元的大小寫被忽略。
當前安裝的 C locale 可能會接受其他數字格式。
如果 base
的值為 0,則自動檢測數字基數:如果字首是 0
,則基數是八進位制;如果字首是 0x
或 0X
,則基數是十六進位制;否則基數是十進位制。
如果負號是輸入序列的一部分,則從數字序列計算出的數值將被取反,就像結果型別中的一元減號一樣。
如果 pos 不是空指標,則 ptr 將接收 str.c_str() 中第一個未轉換字元的地址,並且將計算該字元的索引並存儲在 *pos 中,給出由轉換處理的字元數。
目錄 |
[編輯] 引數
str | - | 要轉換的字串 |
pos | - | 用於儲存已處理字元數的整數地址 |
base | - | 數字基數 |
[編輯] 返回值
對應於 str 內容的整數值。
[編輯] 異常
- 如果沒有執行任何轉換,則丟擲 std::invalid_argument。
- 如果轉換後的值超出結果型別的範圍,或者如果底層函式(std::strtol 或 std::strtoll)將 errno 設定為 ERANGE,則丟擲 std::out_of_range。
[編輯] 示例
執行此程式碼
#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 << "\nCalling with different radixes:\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 Calling with different radixes: 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 2009 | C++11 | 如果發生以下情況,則不會丟擲 std::out_of_range std::strtol 或 std::strtoll 將 errno 設定為 ERANGE |
將丟擲 |
[編輯] 另請參閱
(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 (函式) |