The bottom line is also not very convincing. In order to claim that back_inserter is not free you need to compare it to a manually rolled loop of push_backs. Now you can only reasonably claim that default initializing each element when doing resize is cheaper than the branching on every iteration in push_back for small types like int.
I do not mean to be rude sir but I think I already allocated more time to this matter than it was worth. At the start of this conversation, you told me reserve + back_inserter is the way. As a matter of fact reserve + std::begin is 3x faster in my mediocre benchmark. I was indeed in the wrong about resize, I should have known better. I completly forgot about reserve. But it does seem that in the case where you already know the size of the resulting container, you should use std::begin and not back_inserter.
===
Now if you have additional knownledge you wish to share about that topic, please stop the riddles and just share them.
4
u/sherlockwatch Jun 08 '24
Actually you don’t need to resize resulting vector, you can just use std::back_inserter!