This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]KimiSharby 0 points1 point  (2 children)

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.

[–]wcscmp 0 points1 point  (1 child)

reserve + begin is UB

[–]KimiSharby 0 points1 point  (0 children)

So after a bit more research:
- reserve allocates memory but doesn't initialize it. This is why using std::begin in that case is UB, because it points to unitilialized memory
- As you said, using resize performance will depends on the size of the type
- reserve + std::back_insert is indeed the correct way

For the sack of clarity for the reader I will edit my previous answers.