Is there a way to create a string of say 256 characters at initialization with c++?
Part of my assignment requires me to “1. Create a string of 256 characters. Use repetitions of your first name.”
I’m not quite sure of how to do this other than using a loop but I feel like there is an easier way.
Taking a look at the constructor reference of
basic_string, one can see that there is no easy way of repeating a complete string. For a single character, you could use(2)like this:For generating a string repetition, you’ll need some workaround. It’s easier to do this post-construction by simply filling the string with, for example,
std::generate(having fun with algorithms).Live example.