std::from_chars
Defined in header <utility> | | |
|---|---|---|
std::from_chars_result from_chars(const char* first, const char* last, /*see below*/& value, int base = 10); | (1) | (since C++17) |
std::from_chars_result from_chars(const char* first, const char* last, float& value, std::chars_format fmt = std::chars_format::general); | (2) | (since C++17) |
std::from_chars_result from_chars(const char* first, const char* last, double& value, std::chars_format fmt = std::chars_format::general); | (3) | (since C++17) |
std::from_chars_result from_chars(const char* first, const char* last, long double& value, std::chars_format fmt = std::chars_format::general); | (4) | (since C++17) |
struct from_chars_result { const char* ptr; std::error_code ec; }; | (5) | (since C++17) |
分析字符序列[first,last)用于下面描述的模式。如果没有任何字符与模式匹配,或者解析匹配字符所获得的值在value,,,value,否则匹配模式的字符将被解释为算术值的文本表示形式,该值存储在value...
1%29整数解析器:期望模式与std::strtol在默认的%28“C”%29区域设置和给定的非零数字基中,除了基16不能识别“0x”或“0x”前缀外,只有减号才能识别%28,而不是加号%29,而且只对有符号的整数类型识别。value.范围内的数字10..35%28包括%29表示为小写字符a..z%28大写数字不能识别%29。库为所有有符号和无符号整数类型以及char作为参数的引用类型。value...
2-4%29个浮点分析器:期望模式与std::strtod在默认的%28“C”%29区域设置中,除了
- 加号不能识别%28,只允许减号%29。
- 如果
fmt有std::chars_format::scientific定但不std::chars_format::fixed,则要求指数部分为%28,否则为可选的%29。
- 如果
fmt有std::chars_format::fixed定但不std::chars_format::scientific,则不允许使用可选指数。
- 如果
fmt是std::chars_format::hex,前缀“0x”或“0x”不允许%28字符串“0x123”解析为值“0”,其余数为“x 123”%29。
无论如何,结果值最多是两个浮点值中最接近与模式匹配的字符串的值之一。
5%29返回类型%28参见返回值低于%29
参数
first, last | - | valid character range to parse |
|---|---|---|
value | - | the out-parameter where the parsed value is stored if successful |
base | - | integer base to use: a value between 2 and 36 (inclusive). |
fmt | - | floating-point formatting to use, a bitmask of type std::chars_format |
返回值
在成功时,返回一个类型的值。from_chars_result使...ptr点的第一个字符不匹配模式,或其值等于last如果所有字符匹配ec是false当转换为bool...
如果没有模式匹配,则返回类型的值。from_chars_result使...ptr等号first和ec等号std::errc::invalid_argument...value是未经修改的。
如果模式匹配,但解析的值不在可由value,返回类型的值。from_chars_result使...ec等号std::errc::result_out_of_range和ptr指向第一个字符不匹配的模式。value是未经修改的。
例外
%280%29
注记
与C++和C库中的其他解析函数不同,std::from_chars是区域无关的,不分配的,不抛的。其他库%28使用的解析策略只有一小部分,例如std::sscanf提供%29。这是为了允许在常见的高吞吐量上下文(如基于文本的交换%28 JSON或XML%29)中使用最快的实现。
保证std::from_chars格式的浮点值都可以恢复。to_chars只有当两个函数来自同一个实现时,才会提供准确的。
例
另见
stoistolstoll (C++11)(C++11)(C++11) | converts a string to a signed integer (function) |
|---|---|
stofstodstold (C++11)(C++11)(C++11) | converts a string to a floating point value (function) |
strtolstrtoll | converts a byte string to an integer value (function) |
strtofstrtodstrtold | converts a byte string to a floating point value (function) |
scanffscanfsscanf | reads formatted input from stdin, a file stream or a buffer (function) |
operator>> | extracts formatted data (public member function of std::basic_istream) |
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

