std::format
来自cppreference.com
| 定义于头文件 <format>
|
||
| template<class... Args> std::string format(std::string_view fmt, const Args&... args); |
(1) | (C++20 起) |
| template<class... Args> std::wstring format(std::wstring_view fmt, const Args&... args); |
(2) | (C++20 起) |
| template<class... Args> std::string format(const std::locale& loc, std::string_view fmt, const Args&... args); |
(3) | (C++20 起) |
| template<class... Args> std::wstring format(const std::locale& loc, std::wstring_view fmt, const Args&... args); |
(4) | (C++20 起) |
按照格式字符串 fmt 格式化 args ,并返回作为 string 的结果。 loc 若存在,则用于本地环境特定的格式化。
若对 Args 中的任一 Ti , std::formatter<Ti, CharT> 不满足格式化器 (Formatter) 要求则行为未定义,其中 CharT 是 decltype(fmt)::char_type (对重载 (1,3) 为 char ,对重载 (2,4) 为 wchar_t )。
参数
| fmt | - | 表示格式字符串的字符串视图。
每个替换域拥有下列格式:
arg-id 指定用于格式化其值的 格式说明由对应参数特化的 std::formatter 定义。
|
| args... | - | 要格式化的参数 |
| loc | - | 用于本地环境特定格式化的 std::locale |
返回值
保有格式化结果的 string 对象。
异常
若 fmt 对于提供的参数不是合法的格式字符串则抛出 std::format_error 。并且会传播任何格式化器所抛的异常。
注解
提供多于格式字符串所要求的参数不是错误:
std::format("{} {}!", "Hello", "world", "something"); // OK :产生 "Hello world!"
示例
| 本节未完成 原因:暂无示例 |
参阅
| (C++20) |
通过输出迭代器写其参数的格式化表示 (函数模板) |
| (C++20) |
通过输出迭代器写其参数的格式化表示,不超出指定的大小 (函数模板) |