std::from_chars
定義於標頭檔案 <charconv> |
||
std::from_chars_result from_chars( const char* first, const char* last, |
(1) | (C++17 起) (constexpr since C++23) |
std::from_chars_result from_chars( const char* first, const char* last, |
(2) | (C++17 起) |
解析字元序列 [
first,
last)
以匹配下述模式。如果沒有任何字元匹配模式,或透過解析匹配字元獲得的值無法表示為 value 的型別,則 value 保持不變,否則將匹配模式的字元解釋為算術值的文字表示,並存儲在 value 中。
- 如果 base 為 16,則不識別 "0x" 或 "0X" 字首。
- 只識別負號(不識別正號),且僅適用於 value 的有符號整型。
- 不忽略前導空格。
- 在指數之外不識別正號(開頭只允許負號)
- 如果
fmt
設定了 std::chars_format::scientific 但未設定 std::chars_format::fixed,則指數部分是必需的(否則是可選的) - 如果
fmt
設定了 std::chars_format::fixed 但未設定 std::chars_format::scientific,則不允許可選的指數 - 如果
fmt
是 std::chars_format::hex,則不允許字首 "0x" 或 "0X"(字串 "0x123" 被解析為值 "0",未解析的餘數是 "x123") - 不忽略前導空格。
目錄 |
[編輯] 引數
first, last | - | 要解析的有效字元範圍 |
value | - | 如果成功,則儲存解析值的輸出引數 |
base | - | 要使用的整數基數:介於 2 到 36(含)之間的值。 |
fmt | - | 要使用的浮點格式,型別為 std::chars_format 的位掩碼 |
[編輯] 返回值
成功時,返回型別為 std::from_chars_result 的值,其中 ptr
指向第一個不匹配模式的字元,或者如果所有字元都匹配且 ec
已值初始化,則其值等於 last。
如果未匹配模式,則返回型別為 std::from_chars_result 的值,其中 ptr
等於 first 且 ec
等於 std::errc::invalid_argument。value 未修改。
如果模式匹配,但解析值不在 value 型別可表示的範圍內,則返回型別為 std::from_chars_result 的值,其中 ec
等於 std::errc::result_out_of_range 且 ptr
指向第一個不匹配模式的字元。value 未修改。
[編輯] 異常
不丟擲任何異常。
[編輯] 註解
與 C++ 和 C 庫中的其他解析函式不同,std::from_chars
是與語言環境無關、不分配記憶體、不丟擲異常的。它只提供了其他庫(如 std::sscanf)使用的一小部分解析策略。這樣做是為了實現最快的可能實現,這在常見的高吞吐量上下文(如基於文字的交換(JSON 或 XML))中非常有用。
僅當 std::from_chars
和 std::to_chars 兩個函式來自同一個實現時,才保證 std::from_chars
能夠精確地恢復由 std::to_chars 格式化的每個浮點值。
由一個符號組成但後面沒有數字的模式被視為未匹配任何內容的模式。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_to_chars |
201611L |
(C++17) | 基本字串轉換(std::from_chars , std::to_chars) |
202306L |
(C++26) | 測試<charconv>函式的成功或失敗 | |
__cpp_lib_constexpr_charconv |
202207L |
(C++23) | 為整型 constexpr 的 std::from_chars 和 std::to_chars 過載新增修飾符 |
[編輯] 示例
#include <cassert> #include <charconv> #include <iomanip> #include <iostream> #include <optional> #include <string_view> #include <system_error> int main() { for (std::string_view const str : {"1234", "15 foo", "bar", " 42", "5000000000"}) { std::cout << "String: " << std::quoted(str) << ". "; int result{}; auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result); if (ec == std::errc()) std::cout << "Result: " << result << ", ptr -> " << std::quoted(ptr) << '\n'; else if (ec == std::errc::invalid_argument) std::cout << "This is not a number.\n"; else if (ec == std::errc::result_out_of_range) std::cout << "This number is larger than an int.\n"; } // C++23's constexpr from_char demo / C++26's operator bool() demo: auto to_int = [](std::string_view s) -> std::optional<int> { int value{}; #if __cpp_lib_to_chars >= 202306L if (std::from_chars(s.data(), s.data() + s.size(), value)) #else if (std::from_chars(s.data(), s.data() + s.size(), value).ec == std::errc{}) #endif return value; else return std::nullopt; }; assert(to_int("42") == 42); assert(to_int("foo") == std::nullopt); #if __cpp_lib_constexpr_charconv and __cpp_lib_optional >= 202106 static_assert(to_int("42") == 42); static_assert(to_int("foo") == std::nullopt); #endif }
輸出
String: "1234". Result: 1234, ptr -> "" String: "15 foo". Result: 15, ptr -> " foo" String: "bar". This is not a number. String: " 42". This is not a number. String: "5000000000". This number is larger than an int.
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2955 | C++17 | 此函式位於 <utility> 並使用 std::error_code | 移至 <charconv> 並使用 std::errc |
LWG 3373 | C++17 | std::from_chars_result 可能包含額外的成員 |
禁止額外成員 |
[編輯] 另請參閱
(C++17) |
std::from_chars 的返回型別 (類) |
(C++17) |
將整數或浮點值轉換為字元序列 (函式) |
(C++11)(C++11)(C++11) |
將字串轉換為有符號整數 (函式) |
(C++11)(C++11)(C++11) |
將字串轉換為浮點值 (函式) |
(C++11) |
將位元組字串轉換為整數值 (函式) |
將位元組字串轉換為浮點值 (函式) | |
從 stdin、檔案流或緩衝區讀取格式化輸入 (函式) | |
提取格式化資料 ( std::basic_istream<CharT,Traits> 的公共成員函式) |