Introduction to Stacks using std::stack

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?

Illustration representing computer hardware

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.

Answers to questions are automatically generated and may not have been reviewed.

Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved