In python, the following instruction: print 'a'*5
would output aaaaa
. How would one write something similar in C++ in conjunction with std::ostream
s in order to avoid a for
construct?
To repeat a string n times, we can use the for loop in C++.
The getchar() and putchar() Functions This function reads only single character at a time.
In C++20 you'll be able to use std::format
to do this:
std::cout << std::format("{:a<5}", "");
Output:
aaaaa
In the meantime you can use the {fmt} library, std::format
is based on. {fmt} also provides the print
function that makes this even easier and more efficient (godbolt):
fmt::print("{:a<5}", "");
Disclaimer: I'm the author of {fmt} and C++20 std::format
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With