网站建设基本流程 dns,sem竞价广告,长丰县重点工程建设管理局网站,宁波seo推荐运营培训在标头format定义 ()功能很强大#xff0c;它把字符串当成一个模板#xff0c;通过传入的参数进行格式化#xff0c;并且使用大括号‘{}’作为特殊字符代替‘%’。 1、基本用法
#xff08;1#xff09;不带编号#xff0c;即“{}”#xff08;2#xff09;带…在标头format定义 ()功能很强大它把字符串当成一个模板通过传入的参数进行格式化并且使用大括号‘{}’作为特殊字符代替‘%’。 1、基本用法
1不带编号即“{}”2带数字编号可调换顺序即“{1}”、“{2}”
std::string str std::format({} {}!, Hello, world, something); // OK产生 Hello worldstd::string str std::format({0} {1}!, Hello, world, something); // OK产生 Hello worldstd::string str std::format({1} {0}!, Hello, world, something); // OK产生 world Hello! 不带编号{}默认按参数顺序输入带序号“{2}”则 按参数索引位置输入。 2、复杂点用法
格式说明下面格式都是可选的都不填的话 就等于用 上面的基本用法
[: 填充与对齐 (可选) 正负号 (可选) #(可选) 0(可选) 宽度 (可选) 精度 (可选) L(可选) 类型 (可选)]
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
char c 120;
auto s1 std::format({:06d}, c); // s1 的值是 00120
auto s1 std::format({:06d}, c); // s1 的值是 000120
//对于long long 类型 用{:020d} 超过int类型4个字节 也能正确打印但按下面方式 不指定类型打印应该没问题
//宽度20 不足补0
long long num4294967296123;
string strstd::format({:020},num);
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精度 5auto b1 std::format({:{}f}, pi, 10.0); // 抛出宽度不是整数类型
auto b2 std::format({:{}f}, pi, -10); // 抛出宽度为负
auto b3 std::format({:.{}f}, pi, 5.0); // 抛出精度不是整数类型 格式控制符 和c语言的printf类似 std::format是C20中引入的一种新的字符串格式化方法。它使用类似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}指针表示{:%%}输出一个百分比符号%