std::from_chars
定义于头文件 <charconv> |
||
std::from_chars_result from_chars( const char* first, const char* last, |
(1) | (C++17 起) (constexpr 从 C++23 起) |
std::from_chars_result from_chars( const char* first, const char* last, |
(2) | (C++17 起) |
分析字符序列 [
first,
last)
以查找下述模式。如果没有任何字符匹配该模式,或者通过解析匹配的字符获得的值无法用 value 的类型表示,则 value 不会被修改;否则,匹配模式的字符会被解释为算术值的文本表示,并存储在 value 中。
- "0x" 或 "0X" 前缀在 base 为 16 时不被识别
- 仅识别减号(不识别加号),且仅适用于 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
指向第一个不匹配模式的字符,或者如果所有字符都匹配,则其值等于 last 且 ec
已值初始化。
如果没有模式匹配,则返回 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 格式化的每个浮点值。
由一个符号组成且后面没有数字的模式被视为不匹配任何内容的模式。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_to_chars |
201611L |
(C++17) | 基本字符串转换(std::from_chars , std::to_chars) |
202306L |
(C++26) | 测试 <charconv> 函数的成功或失败 | |
__cpp_lib_constexpr_charconv |
202207L |
(C++23) | 向用于整数类型的 std::from_chars 和 std::to_chars 重载添加 constexpr 修饰符 |
[编辑] 示例
#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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
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> 的公共成员函数) |