首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::to_chars

Defined in header <utility>

std::to_chars_result to_chars(char* first, char* last, /*see below*/ value, int base = 10);

(1)

(since C++17)

std::to_chars_result to_chars(char* first, char* last, float value); std::to_chars_result to_chars(char* first, char* last, double value); std::to_chars_result to_chars(char* first, char* last, long double value);

(2)

(since C++17)

std::to_chars_result to_chars(char* first, char* last, float value, std::chars_format fmt); std::to_chars_result to_chars(char* first, char* last, double value, std::chars_format fmt); std::to_chars_result to_chars(char* first, char* last, long double value, std::chars_format fmt);

(3)

(since C++17)

std::to_chars_result to_chars(char* first, char* last, float value, std::chars_format fmt, int precision); std::to_chars_result to_chars(char* first, char* last, double value, std::chars_format fmt, int precision); std::to_chars_result to_chars(char* first, char* last, long double value, std::chars_format fmt, int precision);

(4)

(since C++17)

struct to_chars_result { char* ptr; std::error_code ec; };

(5)

(since C++17)

皈依value通过连续填充范围将字符串转换为字符串。[first, last),在哪里[first, last)必须是有效范围。

1%29名整数格式者:value中的数字字符串。base%28没有冗余前导零%29。范围内的数字10..35%28包括%29表示为小写字符a..z如果值小于零,则表示以减号开始。库为所有有符号和无符号整数类型以及类型提供重载。char作为参数的类型value...

将2%29值转换为字符串,就像std::sprintf在默认的%28“C”%29区域设置中。转换说明符是fe%28赞成f在领带%29的情况下,根据最短表示的要求选择:字符串表示由最小数目的字符组成,如果存在%29,则在基点%28之前至少有一个数字,并使用相应的std::from_chars函数准确地恢复值。

3%29与%282%29相同,但为as-if printf指定的转换为f如果fmtstd::chars_format::fixed,,,e如果fmtstd::chars_format::scientific,,,a%28但结果中没有领先的“0x”,则为%29fmtstd::chars_format::hex,和g如果fmtchars_format::general...

4%29与%283%29相同,但参数指定的精度除外precision而不是通过最短的表示要求。

5%29返回类型%28参见返回值低于%29

参数

first, last

-

character range to write to

value

-

the value to convert to its string representation

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

precision

-

floating-point precision to use

返回值

在成功时,返回一个类型的值。to_chars_result使...ecfalse当转换为boolptr是所写字符的一次过尾指针。

错误时,返回类型的值。to_chars_resultstd::errc::value_too_largeec,值的副本lastptr,并留下范围的内容。[first, last)处于未指定的状态。

例外

%280%29

注记

与C++和C库中的其他格式化函数不同,std::to_chars是区域无关的,不分配的,不抛的。只有其他库%28使用的格式化策略的一小部分,例如std::sprintf提供%29。这是为了允许在常见的高吞吐量上下文(如基于文本的交换%28 JSON或XML%29)中使用最快的实现。

保证std::from_chars格式的浮点值都可以恢复。to_chars只有当两个函数来自同一个实现时,才会提供准确的。

另见

to_string (C++11)

converts an integral or floating point value to string (function)

printffprintfsprintfsnprintf (C++11)

prints formatted output to stdout, a file stream or a buffer (function)

operator<<

inserts formatted data (public member function of std::basic_ostream)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券