如何连接格式字符串?
How to concatenate the format string?
我想连接两个字符串并将它们用作fmt
库的格式字符串。 一个明显的选择是简单地将它们连接为常规字符串,然后将它们传递到库中:
template<typename... Args>
inline void catfmt(std::string a, std::string b, const Args &... args) {
std::string c = a+b;
fmt::print(c, args...);
}
但是,c
将被丢弃。所以也许 fmt 提供了一种完全跳过它的方法?有没有办法将字符串放入消息中并告诉库它也应该被解析?像这样:
template<typename... Args>
inline void catfmt(std::string a, std::string b, const Args &... args) {
fmt::print("{:sf}{:sf}", a, b, args...);
}
{:sf}
的意思是:放置一个字符串并将其用作格式。
有没有办法将字符串放入消息中并告诉库它也应该被解析?
没有这样的办法。您必须使用operator+
连接它,这是对格式的嵌套调用
fmt::print(fmt::runtime(fmt::format("{}{}", a, b)), args...);
或其他方法。
相关文章:
- 是否可以从格式字符串中检索"width"
- 我应该如何从 stdin C++ 中读取可变长度的格式字符串?
- 从用户定义的类生成格式字符串?
- 如何连接格式字符串?
- 如何在C++中将"%Y%m%d"格式字符串转换为time_t变量?
- CPPCHECK C++成员函数上的格式字符串
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 自定义 {fmt} 格式化函数,具有编译时格式字符串检查功能
- '|' scanf 格式字符串是什么意思
- 如何使用 msgfmt 验证格式字符串中的位置表示法占位符
- sscanf_s:格式字符串"%d"需要类型为"int *"的参数,但可变参数 4 的类型为"WORD *"
- C4477: 'fprintf' :格式字符串 '%s' 需要类型为"char *"的参数,但可变参数 1 的类型为"int *"
- 如何使用OpenSSL API从其PEM格式字符串中读取RSA公钥
- C 格式字符串宏
- C++等效于 C 的格式字符串
- 如何使用 C++ 中的 rapidjson 库从 json 格式字符串中获取值
- _vsnprintf和_vsnprintf_s的格式字符串中的可用百分比字符
- 如何将格式字符串发送到iostream
- 跨平台格式字符串
- 使用 swprintf() 打印"0x0"的格式字符串应该是什么?