std::stoul, std::stoull
來自 cppreference.com
< cpp | string | basic_string
定義於標頭檔案 <string> |
||
unsigned long stoul ( const std::string& str, std::size_t* pos = nullptr, int base = 10 ); |
(1) | (C++11 起) |
unsigned long stoul ( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 ); |
(2) | (C++11 起) |
unsigned long long stoull( const std::string& str, std::size_t* pos = nullptr, int base = 10 ); |
(3) | (C++11 起) |
unsigned long long stoull( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 ); |
(4) | (C++11 起) |
解釋字串 str 中的無符號整數值。
令 ptr 為內部(轉換函式內部)的指標,型別分別為 char* (1,3) 或 wchar_t* (2,4)。
1) 呼叫 std::strtoul(str.c_str(), &ptr, base)。
2) 呼叫 std::wcstoul(str.c_str(), &ptr, base)。
3) 呼叫 std::strtoull(str.c_str(), &ptr, base)。
4) 呼叫 std::wcstoull(str.c_str(), &ptr, base)。
丟棄任何空白字元(透過呼叫 std::isspace 識別),直到找到第一個非空白字元,然後儘可能多地獲取字元以形成有效的 *n 進位制*(n=base
)無符號整數表示,並將其轉換為整數值。有效的無符號整數值由以下部分組成:
- (可選) 加號或減號
- (可選) 字首(
0
)表示八進位制基數(僅當基數為 8 或 0 時適用) - (可選) 字首(
0x
或0X
)表示十六進位制基數(僅當基數為 16 或 0 時適用) - 一系列數字
`base` 的有效值集合是 `{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 | - | 數字基數 |
[編輯] 返回值
轉換為指定無符號整數型別的字串。
[編輯] 異常
- 如果沒有執行任何轉換,則丟擲 std::invalid_argument。
- 如果轉換後的值超出結果型別的範圍,或如果底層函式(std::strtoul 或 std::strtoull)將 errno 設定為 ERANGE,則丟擲 std::out_of_range。
[編輯] 示例
本節不完整 原因:無示例 |
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2009 | C++11 | 如果滿足以下條件,則不會丟擲 std::out_of_range std::strtoul 或 std::strtoull 將 errno 設定為 ERANGE |
將丟擲 |
[編輯] 參見
(C++11)(C++11)(C++11) |
將字串轉換為有符號整數 (函式) |
(C++11)(C++11)(C++11) |
將字串轉換為浮點值 (函式) |