Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

They compile down to the same code on a simple example in release.

Post increment: https://godbolt.org/g/k5rcPA

Pre increment: https://godbolt.org/g/WgfMzU



For std::vector, they should compile to the same thing, the iterator is just a pointer to a flat array. For std::map (ie. red-black tree) or something more complex, it might make a bigger difference. But even then, the iterator is quite light weight (just a pointer) so copying it is not a big deal.


Even for std::map gcc generates identical code:

https://godbolt.org/g/C6kooc

https://godbolt.org/g/7RS5PQ

godbolt is a great resource to see just how good modern compilers are.


I wouldn't say "even" -- that's just another type where the iterator is a trivially copyable/destructable object.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: