Reputation: 30058
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.
Upvotes: 1
Views: 1881
Reputation: 155487
The shorter code is to use fmt
to do the printing directly, rather than just using it to format, then passing the result to normal cout
the iostream
way:
fmt::print("{}\n", fmt::join(v, ", ")); // Optionally, I believe you can use the literals to make the format string compile-type checked, "{}\n"_format
The format string incorporates the newline as well, and print
does both the formatting and printing, so it's markedly shorter/less complex than:
std::cout << fmt::format("{}", fmt::join(v, ", ")) << "\n";
Upvotes: 5