当前位置:网站首页>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;
}
边栏推荐
- WinForm(五)控件和它的成员
- Sub-database sub-table ShardingSphere-JDBC notes arrangement
- 力扣------用栈操作构建数组
- 微信小程序强制更新版本
- 【openpyxl】过滤和排序
- Elastic scaling of construction resources
- Successfully resolved raise TypeError('Unexpected feature_names type')TypeError: Unexpected feature_names type
- ② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
- Apache Commons Configuration Remote Code Execution Vulnerability (CVE-2022-33980) Analysis & Reproduction
- 力扣------使用最小花费爬楼梯
猜你喜欢

sed of the Three Musketeers of Shell Programming

leetcode 前K个高频单词

微信小程序自定义navigationBar

Linux安装redis数据库

More parameter exposure of Pico 4: Pancake + color perspective, and Pro version

Word set before the title page

容器技术真的是环境管理的救星吗?

③ 关系数据库标准语言SQL 数据查询(SELECT)

Web APIs BOM - A Comprehensive Case of Operating Browsers

#yyds干货盘点#【愚公系列】2022年08月 Go教学课程 008-数据类型之整型
随机推荐
使用mysql语句操作数据表(table)
Shell编程三剑客之sed
力扣------值相等的最小索引
loop word
【爬虫】scrapy创建运行爬虫、解析页面(嵌套url)、自定义中间件(设置UserAgent和代理IP)、自定义管道(保存到mysql)
leetcode 前K个高频单词
EN 12467纤维水泥平板产品—CE认证
Only lazy and hungry. You still don't understand the singleton pattern!
构建资源的弹性伸缩
【pypdf2】合并PDF、旋转、缩放、裁剪、加密解密、添加水印
C# JObject解析JSON数据
Apache Commons Configuration远程代码执行漏洞(CVE-2022-33980)分析&复现
池化技术有多牛?来,告诉你阿里的Druid为啥如此牛逼!
How engineers treat open source
单片机人机交互--矩阵按键
② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
Still using Xshell?You are out, recommend a more modern terminal connection tool, easy to use!
ABP中的数据过滤器
[GXYCTF2019]BabySQli
[21 Days Learning Challenge] Half Insertion Sort