In the language I was first introduced to, there was a function repeat()
, that took a string, and repeated it n
times. For example, repeat ("hi", 3)
gives a result of "hihihi"
.
I did have quite a few times that I used this function, but to my dismay, I've never found something similar in C++. Yes, I can easily make my own, or make it easier to use, but I'm kind of surprised that it isn't included already.
One place it would fit in really well is in std::string
:
std::string operator* (const std::string &text, int repeatCount);
std::string operator* (int repeatCount, const std::string &text);
That would allow for syntax such as:
cout << "Repeating \"Hi\" three times gives us \"" << std::string("Hi") * 3 << "\"."
Now that in itself isn't all too great yet, but it could be better, which brings me to my other part: literals.
Any time we use the string operators, such as operator+
, we have to make sure one argument is actually a string. Why didn't they just define a literal for it, like ""s
? Literal suffixes not beginning with an underscore are reserved for the implementation, so this shouldn't conflict seeing as how this could have been added before anyone actually started making their own.
Coming back to the repeat example, the syntax would simply be:
cout << "123"s * 3 + "456"s;
This would produce:
123123123456
While at it, one for characters could be included as well, to satisfy cout << '1's + '2's;
Why weren't these two features included? They definitely have a clear meaning, and make coding easier, while still using the standard libraries.