1. std::format
format_01.cpp
// g++ format_01.cpp -std=c++20
#include <iostream>
#include <string>
#include <format>void test_01() {// 使用字符串填充std::cout << std::format("Hello {}!\n", "World"); // Hello World!// 提供的参数可以比placeholder多std::cout << std::format("Hello {}!\n", "World", 1, 2, 3); // Hello World!// 可以通过占位符调整输出的顺序std::cout << std::format("{0} {1} {2} {3}\n", "Wang", "Zai", "Niu", "Nai"); // Wang Zai Niu Naistd::cout << std::format("{3} {2} {0} {1}\n", "Wang", "Zai", "Niu", "Nai"); // Nai Niu Wang Zaistd::cout << std::format("{0} ", "Hao", "Yu", "Zhi", "Shi", "Jie"); // Haostd::cout << std::format("{1} ", "Hao", "Yu", "Zhi", "Shi", "Jie"); // Yustd::cout << std::format("{2} ", "Hao", "Yu", "Zhi", "Shi", "Jie"); // Zhistd::cout << std::format("{3} ", "Hao", "Yu", "Zhi", "Shi", "Jie"); // Shistd::cout << std::format("{4}\n", "Hao", "Yu", "Zhi", "Shi", "Jie"); // Jie// 使用整数填充int a = 1, b = 2;int c = a + b;std::cout << std::format("{} + {} = {}\n", a, b, c); // 1 + 2 = 3// 使用浮点数填充double d = 1.2, e = 3.5;double f = d + e;std::cout << std::format("{0} + {1} = {2}\n", d, e, f); // 1.2 + 3.5 = 4.7
}int main() {test_01();
}
format_02.cpp
// g++ format_02.cpp -std=c++20
#include <iostream>
#include <string>
#include <format>void test_02(){/* {[arg_id][!spec][:[fill]align][sign][#][0][width][.precision][type]} */// 冒号前面的2表示占位符arg_id,输出第二个参数;// “:*^+100”中,^是fill表示居中对齐,20是align表示填充宽度,*是填充字符,+表示加法符号std::cout << std::format("{2:*^+20}\n", 56, 24, 36); // ********+36*********// 填充宽度也可以用占位符、参数对来表示,如下面的3表示使用占位符的第三个参数作为填充宽度std::cout << std::format("{2:*^+{3}}\n", 56, 24, 36, 20); // ********+36*********// 左右对齐std::cout << std::format("{0:}: {2:*<20}\n", "left-align", "right-align", -36); // left-align: -36*****************std::cout << std::format("{1:}: {2:0>20}\n", "left-align", "right-align", 36); // right-align: 00000000000000000036// 十六进制、十进制、八进制、二进制std::cout << std::format("{0:}: {4:^10X}\n", "HEX", "DEC", "OCT", "BIN", 15); // HEX: F std::cout << std::format("{0:}: {4:^#10X}\n", "HEX", "DEC", "OCT", "BIN", 15); // HEX: 0XF std::cout << std::format("{0:}: {4:^#10x}\n", "HEX", "DEC", "OCT", "BIN", 15); // HEX: 0xf std::cout << std::format("{3:}: {4:^#10B}\n", "HEX", "DEC", "OCT", "BIN", 15); // BIN: 0B1111std::cout << std::format("{3:}: {4:^#10b}\n", "HEX", "DEC", "OCT", "BIN", 15); // BIN: 0b1111 std::cout << std::format("{2:}: {4:^#10o}\n", "HEX", "DEC", "OCT", "BIN", 15); // OCT: 017std::cout << std::format("{1:}: {4:^#10}\n", "HEX", "DEC", "OCT", "BIN", 15); // DEC: 15// 精度double val = 3.14159265358 / 1.3;std::cout << std::format("{0:*^20.12}\n", val); // ***2.41660973352****std::cout << std::format("{0:*^20.12f}\n", val); // ***2.41660973352****std::cout << std::format("{0:.12g}\n", val); // g是默认格式,2.41660973352std::cout << std::format("{0:.12e}\n", val); // 2.416609733523e+00std::cout << std::format("{0:.12E}\n", val); // 2.416609733523E+00std::cout << std::format("{0:.12a}\n", val); // 1.355377be56a4p+1std::cout << std::format("{0:.12A}\n", val); // 1.355377BE56A4P+1int width = 20, precision = 12; // 总共20位,精度(小数位)占12位std::cout << std::format("{0:=^{1}.{2}E}\n", val, width, precision); // =2.416609733523E+00=
}int main() {test_02();
}
2. std::format_to
、std::format_to_n
、std::formatted_size
format_03.cpp
#include <string>
#include <iostream>
#include <format>
#include <vector>void test_03(){double pi = 3.14159265358979;char content[50] = {'\0'};sprintf(content, "%f", pi);printf("%s\n", content); // 3.141593std::cout << std::format("{}\n", pi);std::format_to_n(content, sizeof(content), "{}", pi);std::cout << content << std::endl; // 3.14159265358979std::vector<char> vec1;std::format_to(std::back_inserter(vec1), "{}", pi); // 3.14159265358979for(const auto&it: vec1){ std::cout << it;}std::cout << std::endl;// 计算格式化字符串需要的空间auto size = std::formatted_size("{}", pi);std::vector<char> vec2(size); std::format_to(vec2.data(), "{}", pi); // 3.14159265358979for(const auto&it: vec2){ std::cout << it; }std::cout << std::endl;
}int main() {test_03();
}