Fast string duplication
Preallocate memory for the result
s = “”;s.reserve(x.length() * n);while (--n >= 0) s += x;
Advantage: O(n) time instead of O(n2)
Disadvantage: requires cooperation with string class
Previous slide
Next slide
Back to first slide
View graphic version