使用FMT的格式范围而无需封闭[]括号
我想用,
saparator打印一系列值,但没有[]
使用FMT库。
如果我尝试打印出这样的范围,那么
std::vector v {8,4,7,2};
std::cout << fmt::format("{}",v) <<"\n";
输出为
[8、4、7、2]
但是我实际上希望输出为
8、4、7、2
我该怎么做?
我发现的一种方法是执行以下操作
std::cout << fmt::format("{}",fmt::join(v,", ")) <<"\n";
。但是,它是冗长的,它给FMT打了2个电话, 我想避免。这可能吗?
另外,由于性能和可读性原因,我对删除[] 来自FMT产生的字符串。
I want to print a range of values with ,
separator but without []
using fmt library.
If I try to print out some range like this
std::vector v {8,4,7,2};
std::cout << fmt::format("{}",v) <<"\n";
the output is
[8, 4, 7, 2]
But I actually want the output to be
8, 4, 7, 2
How can I do this?
One approach I've found is to do the following
std::cout << fmt::format("{}",fmt::join(v,", ")) <<"\n";
which does work. However, it's verbose, and it makes 2 calls to fmt,
which I would like to avoid. Is this possible?
Also, due to performance and readability reasons I am not interested in removing []
from the string produced by fmt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
较短的代码是使用
fmt
直接执行打印,而不是仅将其用于格式化,然后将结果传递到常规cout
iostream
方法:格式字符串也包含了新线,
print
同时进行格式和打印,因此比以下格式更短/不复杂。The shorter code is to use
fmt
to do the printing directly, rather than just using it to format, then passing the result to normalcout
theiostream
way:The format string incorporates the newline as well, and
print
does both the formatting and printing, so it's markedly shorter/less complex than: