当前位置:网站首页>std::format格式化自定义类型
std::format格式化自定义类型
2022-08-11 01:07:00 【windSnowLi】
示例代码
#include <string>
#include <iostream>
// 导入format
#include <format>
struct Vector4D
{
int x, y, z, s;
};
// std::formatter格式化Vector4D
namespace std {
template <>
class formatter<Vector4D> {
public:
explicit formatter() noexcept
: _fmt(OutputFormat::XYZS)
{
}
typename std::basic_format_parse_context<char>::iterator
parse(std::basic_format_parse_context<char>& pc) {
if (pc.begin() == pc.end() || *pc.begin() == '}') {
_fmt = OutputFormat::XYZS;
return pc.end();
}
switch (*pc.begin()) {
case 'X':
_fmt = OutputFormat::X;
break;
case 'Y':
_fmt = OutputFormat::Y;
break;
case 'Z':
_fmt = OutputFormat::Z;
break;
case 'S':
_fmt = OutputFormat::S;
default:
throw std::format_error("Invalid format specification");
}
return pc.begin() + 1;
}
template <typename OutputIt>
std::basic_format_context<OutputIt, char>::iterator
format(const Vector4D& value, std::basic_format_context<OutputIt, char>& fc) const noexcept {
std::string valueString;
switch (_fmt) {
case OutputFormat::XYZS: {
valueString = std::format("X={}, Y={}, Z={}, S={}",
value.x, value.y, value.z, value.s);
break;
}
case OutputFormat::X:
valueString = std::format("X={}", value.x);
break;
case OutputFormat::Y:
valueString = std::format("Y={}", value.y);
break;
case OutputFormat::Z:
valueString = std::format("Z={}", value.z);
break;
case OutputFormat::S:
valueString = std::format("S={}", value.s);
break;
}
auto output = fc.out();
for (auto ch : valueString) {
*output++ = ch;
}
return output;
}
private:
enum class OutputFormat {
X,
Y,
Z,
S,
XYZS
};
OutputFormat _fmt;
};
} // namespace std
int main()
{
Vector4D v = {
1, 2, 3, 4 };
std::cout << std::format("{}", v) << std::endl;
return 0;
}
边栏推荐
- [21 Days Learning Challenge] Half Insertion Sort
- EN 12467纤维水泥平板产品—CE认证
- Shell 文本三剑客 Sed
- Difference Between Image Recognition and Semantic Segmentation
- [GXYCTF2019]BabySQli
- How to do patent mining, the key is to find patent points, in fact, it is not too difficult
- 力扣------使用最小花费爬楼梯
- Update chromedriver driver programming skills │ selenium
- 【pypdf2】合并PDF、旋转、缩放、裁剪、加密解密、添加水印
- Successfully resolved TypeError: can't multiply sequence by non-int of type 'float'
猜你喜欢
R语言多元线性回归、ARIMA分析美国不同候选人对经济GDP时间序列影响
Linux install redis database
Apache Commons Configuration远程代码执行漏洞(CVE-2022-33980)分析&复现
Go项目配置管理神器之viper使用详解
③ 关系数据库标准语言SQL 数据查询(SELECT)
数据分析面试手册《统计篇》
WebView2 通过 PuppeteerSharp 实现RPA获取壁纸 (案例版)
More parameter exposure of Pico 4: Pancake + color perspective, and Pro version
Mysql database installation and configuration detailed tutorial
使用mysql语句操作数据表(table)
随机推荐
【ASM】字节码操作 ClassWriter COMPUTE_FRAMES 的作用 与 visitMaxs 的关系
【openpyxl】只读模式、只写模式
ADC和DAC记录
22/8/9 Collection of Greedy Problems
【Video】Report Sharing | 2021 Insurance Industry Digital Insights
[21-day learning challenge - kernel notes] (5) - devmem read and write register debugging
深度解析volatile关键字(保证够全面)
Shell 文本三剑客 Sed
如何破坏Excel文件,让其显示文件已损坏方法
Successfully resolved raise TypeError('Unexpected feature_names type')TypeError: Unexpected feature_names type
关于编程本质那些事
【pypdf2】合并PDF、旋转、缩放、裁剪、加密解密、添加水印
两个链表的第一个公共节点——LeetCode
Go项目配置管理神器之viper使用详解
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 4 ADHK Problem Solving
Exceptions and exception handling mechanisms
Linux install redis database
dump_stack ()
How to easily obtain the citation format of references?
微服务概念