std::stack::emplace vs std::stack::push

What's the difference between std::stack::emplace and std::stack::push? When should I use one over the other?

Both std::stack::emplace and std::stack::push are used to add elements to a stack, but they do it in slightly different ways.

std::stack::push

This function takes an object of the stack's element type and pushes it onto the stack. If the object is an rvalue, it will be moved onto the stack. If it's an lvalue, it will be copied.

std::stack<std::string> s;
std::string str = "Hello";

// str is copied onto the stack
s.push(str);

// temporary string is moved onto the stack
s.push(std::string("World"));

std::stack::emplace

This function constructs a new object directly on the stack, using the arguments passed to emplace as the arguments for the object's constructor. This avoids creating a temporary object and then copying or moving it onto the stack.

std::stack<std::string> s;

// string is constructed directly on the stack
s.emplace("Hello");

When to use emplace over push (or vice versa) depends on your specific use case:

  • If you have an existing object that you want to push onto the stack, you must use push.
  • If you don't have an existing object and you would need to create a temporary object to push onto the stack, emplace can be more efficient, as it constructs the object directly on the stack, avoiding the creation of a temporary.

Here's an example that demonstrates the difference:

#include <iostream>
#include <stack>
#include <string>

int main() {
  std::stack<std::string> s;

  std::cout << "Pushing..." << std::endl;
  // temporary string created and moved
  s.push(std::string("Hello, "));

  std::string world = "world!";
  // world copied onto the stack
  s.push(world);

  std::cout << "Emplacing..." << std::endl;
  // string constructed directly on the stack
  s.emplace("Hello, ");

  // string(5, 'A') constructed on the stack
  s.emplace(5, 'A');
}
Pushing...
Emplacing...

In this code, the first push operation creates a temporary std::string object and then moves it onto the stack. The second push copies the world string onto the stack.

The first emplace operation constructs a string directly on the stack from a const char*. The second emplace operation constructs a string directly on the stack using the constructor std::string(size_t n, char c).

In general, emplace can provide better performance by avoiding temporary objects, but the difference is often negligible unless you're working with very large numbers of objects or very expensive-to-construct objects. The readability and clarity of your code should also be considered when choosing between push and emplace.

Introduction to Stacks using std::stack

An introduction to the stack data structure, and the standard library implementation - std::stack

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

When to use std::stack vs std::vector?
In what situations would I choose to use a std::stack instead of a std::vector in C++? They seem to have similar functionality.
Changing the underlying container of std::stack
What are some reasons I might want to change std::stack's underlying container to something other than std::deque?
Iterating over a std::stack
How can I iterate over all the elements in a std::stack? There don't seem to be any obvious methods for this.
Exception safety with std::stack
What happens if an exception is thrown while I'm modifying a std::stack? Is it exception-safe?
Thread safety with std::stack
Is std::stack thread-safe? Can I safely access a stack concurrently from multiple threads?
Performance considerations with std::stack
What are some performance considerations to keep in mind when using std::stack?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant