std::wcstoimax, std::wcstoumax
來自 cppreference.com
定義於標頭檔案 <cinttypes> |
||
std::intmax_t wcstoimax( const wchar_t* nptr, wchar_t** endptr, int base ); |
(C++11 起) | |
std::uintmax_t wcstoumax( const wchar_t* nptr, wchar_t** endptr, int base ); |
(C++11 起) | |
解析由 nptr 指向的寬字串中的無符號整數值。
跳過任何空白字元(透過呼叫 std::iswspace 識別),直到找到第一個非空白字元,然後儘可能多地獲取字元以形成一個有效的 base-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
,則基數是十六進位制;否則基數是十進位制。
如果減號是輸入序列的一部分,則從數字序列計算出的數值將被取反,如同結果型別中的一元減號,這將應用無符號整數環繞規則。
這些函式將 endptr 所指向的指標設定為指向最後一個被解釋字元之後的寬字元。如果 endptr 是空指標,則忽略它。
目錄 |
[編輯] 引數
nptr | - | 指向要解釋的以 null 結尾的寬字串的指標 |
endptr | - | 指向寬字元指標的指標 |
base | - | 被解釋整數值的*基數* |
[編輯] 返回值
成功時,返回對應於 str 內容的整數值。如果轉換後的值超出相應返回型別的範圍,則發生範圍錯誤並返回 INTMAX_MAX、INTMAX_MIN、UINTMAX_MAX 或 0(視情況而定)。如果無法執行轉換,則返回 0。
[編輯] 示例
執行此程式碼
#include <cinttypes> #include <iostream> #include <string> int main() { std::wstring str = L"helloworld"; std::intmax_t val = std::wcstoimax(str.c_str(), nullptr, 36); std::wcout << str << " in base 36 is " << val << " in base 10\n"; wchar_t* nptr; val = std::wcstoimax(str.c_str(), &nptr, 30); if (nptr != &str[0] + str.size()) std::wcout << str << " in base 30 is invalid." << " The first invalid digit is " << *nptr << '\n'; }
輸出
helloworld in base 36 is 1767707668033969 in base 10 helloworld in base 30 is invalid. The first invalid digit is w
[編輯] 參閱
(C++11)(C++11) |
將位元組字串轉換為 std::intmax_t 或 std::uintmax_t (函式) |
將寬字串轉換為整數值 (函式) | |
將寬字串轉換為無符號整數值 (函式) | |
C 文件 用於 wcstoimax, wcstoumax
|