std::strtoimax, std::strtoumax
來自 cppreference.com
定義於標頭檔案 <cinttypes> |
||
std::intmax_t strtoimax( const char* nptr, char** endptr, int base ); |
(1) | (C++11 起) |
std::uintmax_t strtoumax( const char* nptr, char** endptr, int base ); |
(2) | (C++11 起) |
解釋由 nptr 指向的位元組字串中的整數值。
丟棄所有空白字元(透過呼叫 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
,則基數為十六進位制;否則基數為十進位制。
如果減號是輸入序列的一部分,則從數字序列計算出的數值將被取反,就像結果型別中的一元減號一樣。
函式將 endptr 指向的指標設定為指向解釋的最後一個字元之後的字元。如果 endptr 是空指標,則忽略它。
如果 nptr 為空或不具有預期形式,則不執行轉換,並且(如果 endptr 不是空指標)將 nptr 的值儲存在 endptr 指向的物件中。
目錄 |
[編輯] 引數
nptr | - | 指向要解釋的空終止位元組字串的指標 |
endptr | - | 指向字元指標的指標。 |
base | - | 被解釋整數值的*基數* |
[編輯] 返回值
- 如果成功,返回與 str 內容對應的整數值。
- 如果轉換後的值超出相應返回型別的範圍,則發生範圍錯誤(將 errno 設定為 ERANGE)並返回 INTMAX_MAX、INTMAX_MIN、UINTMAX_MAX 或 0,視情況而定。
- 如果無法執行轉換,則返回 0。
[編輯] 示例
執行此程式碼
#include <cinttypes> #include <iostream> #include <string> int main() { std::string str = "helloworld"; std::intmax_t val = std::strtoimax(str.c_str(), nullptr, 36); std::cout << str << " in base 36 is " << val << " in base 10\n"; char* nptr; val = std::strtoimax(str.c_str(), &nptr, 30); if (nptr != &str[0] + str.size()) std::cout << 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)(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 文件 用於 strtoimax, strtoumax
|