4 ms·
Capacity is required only if you plan to grow a string in place (so you know how much you can add before reallocating). Reallocating is possible here because t
by kazinator 6y ago
Capacity is required only if you plan to grow a string in place (so you know how much you can add before reallocating).
Reallocating is possible here because the string is one object, including the header. Reallocating would change the identity of the object, which is a non-starter when multiple places in the program are sharing the pointer via reference counting.
Therefore, all the strings are immutable here, and so all operations return a new string object.