fmtlib支持 range的基本语法:
range_format_spec ::= [":" [underlying_spec]]
underlying_spec是基于范围引用类型的格式化程序进行解析的。
测试代码:
#include <iostream>
#include <vector>
#include <fmt/ranges.h>
using namespace std;
int main(int argc, char** argv)
{
string x;
x = fmt::format("{}", std::vector<int>{ 10, 20, 30 });
cout << x << endl;
// Result: [10, 20, 30]
x = fmt::format("{::#x}", std::vector<int>{ 10, 20, 30 });
cout << x << endl;
// Result: [0xa, 0x14, 0x1e]
x = fmt::format("{}", vector<char>{ 'h', 'e', 'l', 'l', 'o' });
cout << x << endl;
// Result: ['h', 'e', 'l', 'l', 'o']
x = fmt::format("{::}", vector<char>{ 'h', 'e', 'l', 'l', 'o' });
cout << x << endl;
// Result: [h, e, l, l, o]
x = fmt::format("{::d}", vector<char>{ 'h', 'e', 'l', 'l', 'o' });
cout << x << endl;
// Result: [104, 101, 108, 108, 111]
return 0;
}
运行结果:


本文展示了fmtlib库如何支持对范围类型如vector进行格式化输出,包括默认格式和自定义格式如十六进制显示。示例代码中,演示了对整数和字符向量的不同格式化方法。

被折叠的 条评论
为什么被折叠?



