If I work with strings and concise code matters a bit more than performance (like you have in Python), then I might write just this:
static const std::string pattern = "XY";
std::cout << pattern * n; //repeat pattern n times!
And to support this, I would add this functionality in my string library:
std::string operator * (std::string const & s, size_t n)
{
std::string result;
while(n--) result += s;
return result;
}
One you have this functionality, you can use it elsewhere also:
std::cout << std::string("foo") * 100; //repeat "foo" 100 times!
And if you have user-defined string literal, say _s
, then just write this:
std::cout << "foo"_s * 15; //too concise!!
std::cout << "XY"_s * n; //you can use this in your case!
Online demo.
Cool, isn't it?