For the little "x" simple loop your friend. For large βxβ and relatively short βstrβ, we can think of a βsmarterβ solution by reusing an already concatenated string.
std::string MakeDuplicate( const std::string& str, unsigned int x ) { std::string newstr; if (x>0) { unsigned int y = 2; newstr.reserve(str.length()*x); newstr.append(str); while (y<x) { newstr.append(newstr); y*=2; } newstr.append(newstr.c_str(), (xy/2)*str.length()); } return newstr; }
Or something like that: o) (I think it can be written better, but there is an idea).
EDIT: I was interested and conducted several tests comparing the three solutions on my laptop with the visual studio (reuse option, simple loop with pre-distribution, simple copy and loop-1 without prior use). Expected results: for a small version of the pre-location x (<10), as a rule, the fastest, the pre-location was not the minimum bit slower, since the high speed of the acceleration of the "reuse" version is really significant (log n vs n complexity). It's nice, I just canβt come up with any real problem that could use it: o)
source share