iko
iko

Reputation: 53

C++ {fmt} format dynamically repeat argument

I need to match a file format that heavily relies on tab-based indentation. Is there an idiomatic way to repeatedly insert a character (in my case \t) a given n number of times using {fmt}?

I'm looking for something similar to how the alignment works:

fmt::format("{:>{}}", "right aligned", 30);
// Result: "                 right aligned"

All the solutions I came up with feel contrived.

Upvotes: 3

Views: 5335

Answers (1)

KamilCuk
KamilCuk

Reputation: 141180

The fmt::format string syntax allows to specify a fill character. You can print an empty string and specify the fill character to tabs and then "regulate" how many tabulations are printed.

int count = 10;
fmt::format("{:\t>{}}", "", count);

Upvotes: 5

Related Questions