Say I want to make a string out of a couple different pieces. Say we’ve got some input strings a
and b
and I want to make some kind of debug description, like maybe a: <a>, b: <b>
. Shouldn’t be too hard, right? Slap it together:
std::string result = "a: " + a + ", b: " + b;
Easy, right? How many allocations does this incur, though? It could be more than one. We can do better than that – we know up-front everything going into it, so surely we can allocate the right amount of space up-front. How would that look?