c++20 std::format 格式化说明

在标头<format>定义

()功能很强大,它把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号'{}'作为特殊字符代替'%'。

1、基本用法

  • (1)不带编号,即"{}"
  • (2)带数字编号,可调换顺序,即"{1}"、"{2}"
cpp 复制代码
std::string str = std::format("{} {}!", "Hello", "world", "something"); // OK,产生 "Hello world"

std::string str = std::format("{0} {1}!", "Hello", "world", "something"); // OK,产生 "Hello world"

std::string str = std::format("{1} {0}!", "Hello", "world", "something"); // OK,产生 "world Hello!"

不带编号"{}"默认按参数顺序输入,带序号"{2}"则 按参数索引位置输入。

2、复杂点用法

格式说明:下面格式都是可选的,都不填的话 就等于用 上面的基本用法

cpp 复制代码
[: 填充与对齐 (可选) 正负号 (可选) #(可选) 0(可选) 宽度 (可选) 精度 (可选) L(可选) 类型 (可选)]
cpp 复制代码
char c = 120;

//宽度为6,默认按空格填充
auto s0 = std::format("{0:6}", 42);    // s0 的值是 "    42"
auto s0 = std::format("{:6}", 42);    // s0 的值是 "    42"

//宽度为6,默认按空格填充
auto s1 = std::format("{:6}", 'x');   // s1 的值是 "x     "
//宽度为6 左对齐   用*号填充
auto s2 = std::format("{:*<6}", 'x'); // s2 的值是 "x*****"
//宽度为6 右对齐   用*号填充
auto s3 = std::format("{:*>6}", 'x'); // s3 的值是 "*****x"
//宽度为6 居中对齐 用*号填充
auto s4 = std::format("{:*^6}", 'x'); // s4 的值是 "**x***"
//宽度为6 按数字打印
auto s5 = std::format("{:6d}", c);    // s5 的值是 "   120"
//宽度为6 
auto s6 = std::format("{:6}", true);  // s6 的值是 "true  "
cpp 复制代码
char c = 120;
auto s1 = std::format("{:+06d}", c);   // s1 的值是 "+00120"
auto s1 = std::format("{:06d}", c);   // s1 的值是 "000120"
cpp 复制代码
//对于long long 类型 用"{:020d}" 超过int类型4个字节 也能正确打印,但按下面方式 不指定类型打印应该没问题
//宽度20 不足补0
long long num=4294967296123;
string str=std::format("{:020}",num);
cpp 复制代码
float pi = 3.14f;
auto s1 = std::format("{:10f}", pi);           // s1 = "  3.140000" (宽度 = 10)
auto s2 = std::format("{:{}f}", pi, 10);       // s2 = "  3.140000" (宽度 = 10)
auto s3 = std::format("{:.5f}", pi);           // s3 = "3.14000" (精度 = 5)
auto s4 = std::format("{:.{}f}", pi, 5);       // s4 = "3.14000" (精度 = 5)
auto s5 = std::format("{:10.5f}", pi);         // s5 = "   3.14000"
                                               // (宽度 = 10,精度 = 5)
auto s6 = std::format("{:{}.{}f}", pi, 10, 5); // s6 = "   3.14000"
                                               // (宽度 = 10,精度 = 5)
 
auto b1 = std::format("{:{}f}", pi, 10.0);     // 抛出:宽度不是整数类型
auto b2 = std::format("{:{}f}", pi, -10);      // 抛出:宽度为负
auto b3 = std::format("{:.{}f}", pi, 5.0);     // 抛出:精度不是整数类型

格式控制符 和c语言的printf类似:

cpp 复制代码
std::format是C++20中引入的一种新的字符串格式化方法。它使用类似Python的str.format风格的语法来格式化字符串。

以下是一些常用的std::format格式控制符:

{:b}:二进制表示

{:d}:十进制表示

{:o}:八进制表示

{:x}:十六进制表示,使用小写字母

{:X}:十六进制表示,使用大写字母

{:c}:相应的Unicode字符

{:s}:字符串(实际上是任何可以转换为字符串的类型)

{:a}:浮点数表示,使用科学计数法

{:A}:浮点数表示,使用科学计数法,并且使用大写字母

{:e}:浮点数表示,使用科学计数法,并且使用小写字母

{:E}:浮点数表示,使用科学计数法,并且使用大写字母

{:f}:浮点数表示,不使用科学计数法

{:F}:与{:f}相同,但在某些平台上表现不同

{:g}:根据值自动选择{:f}或{:e}

{:G}:根据值自动选择{:f}或{:E}

{:n}:与{:g}相同,但添加了千位分隔符

{:p}:指针表示

{:%%}:输出一个百分比符号(%)
相关推荐
啦啦啦啦啦zzzz4 天前
一致性哈希
服务器·算法·哈希算法·c++20·分布式存储
keep intensify8 天前
C++20线程池
c++·算法·c++20
繁华的地方不一定留下你的脚印13 天前
C++20 concepts:把模板要求写在入口,而不是等一屏报错
c++20
码匠许师傅13 天前
【C++三方组件】Asio 下篇:C++20 协程版 TCP 客户端与服务端
c++·tcp/ip·c++20
啦啦啦啦啦zzzz1 个月前
利用RAII机制进行日志的采集
linux·服务器·c++·网络编程·c++20
啦啦啦啦啦zzzz1 个月前
Logger的组装(c++20)
c++·算法·c++20
啦啦啦啦啦zzzz1 个月前
Appender抽象类的实现c++20
linux·服务器·c++·c++20
OPEN-F1 个月前
C++20新特性精讲:概念、范围与三路比较
算法·c++20
小小龙学IT1 个月前
C++ std::chrono 时间库深度解析:从 duration 模板到 C++20 日历与时区
linux·c++·c++20
郝学胜-神的一滴1 个月前
C++20 高级编程 004:从初始化、内存到const系列关键字
开发语言·算法·编程·软件构建·c++20