Difference between <queue>'s emplace and push

Dami.h picture Dami.h · Feb 20, 2016 · Viewed 10.3k times · Source

What are the differences between <queue>'s emplace and push?

here's explanation about the std::queue::emplace and std::queue::push .

Both methods add element after its current last element, return None.

Answer

Sami Sallinen picture Sami Sallinen · Feb 20, 2016

push() adds a copy of an already constructed object into the queue as a parameter, it takes an object of the queue's element type.

emplace() constructs a new object in-place at the end of the queue. It takes as parameters the parameters that the queue's element types constructor takes.

If your usage pattern is one where you create a new object and add it to the container, you shortcut a few steps (creation of a temporary object and copying it) by using emplace().