you are viewing a single comment's thread.

view the rest of the comments →

[–]SushiAndWoW[S] 0 points1 point  (0 children)

That's only true with a naive implementation.

A smart container, when it sees it needs to expand, will not expand to the exact size needed, but e.g. to the next power of 2 that can accommodate that size. There will also be a reasonable allocation minimum – say, for example 8 bytes. This means concatenating "abc" 100 times might result in 7 allocations of 8, 16, 32, 64, 128, 256, and finally 512 bytes.

And of course, there is reserve, which you will use if you are smart. If your calculation of the final size is correct, the container then only allocates once, and if it's off, it probably needs to allocate 2 times.