What is the difference between emplace_back and push_back in a std::vector?
Last updated
push_backtakes an already-constructedTand copies or moves it into the vector.emplace_backperfectly forwards constructor arguments to build the element in place, avoiding a temporary. It can also call explicit constructors.For an existing object both cost the same (a move). The gain comes when constructing from raw arguments.
- https://www.youtube.com/watch?v=BbPWrkgj1I4
- https://discourse.llvm.org/t/guidelines-for-use-of-emplace-back-vs-push-back-in-clang/39556
- https://stackoverflow.com/questions/28222736/do-vector-emplace-back-and-vector-push-back-do-the-same-thing
- https://lemire.me/blog/2012/06/20/do-not-waste-time-with-stl-vectors/
- https://pvs-studio.com/en/docs/warnings/v823/
- https://quuxplusone.github.io/blog/2021/03/03/push-back-emplace-back/
- https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3981r0.html
- https://stackoverflow.com/questions/4303513/push-back-vs-emplace-back