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.

While std::stack and std::vector can both be used to store a collection of elements, they are designed for different use cases.

You would choose std::stack when:

  • You need to enforce LIFO (last-in, first-out) access. A stack ensures elements can only be added and removed from one end.
  • You only need access to the top element. If you don't need random access to elements, a stack provides a simpler interface.
  • You want to prevent unintended access. The restricted interface of stack helps prevent bugs from accessing elements incorrectly.

You would choose std::vector when:

  • You need random access to elements using an index. A vector allows accessing any element by its position.
  • You need to iterate over elements. Vectors can be easily iterated using a range-for loop or iterators.
  • You need the last element. While a stack can only access the top element, with a vector you can use back() to get the last element.

Here's an example showing cases where stack and vector are appropriate:

#include <iostream>
#include <stack>
#include <vector>

int main() {
  // Use a stack when LIFO access is needed
  std::stack<int> s;
  s.push(1);
  s.push(2);
  s.push(3);
  while (!s.empty()) {
    std::cout << s.top() << ' ';  // prints 3 2 1
    s.pop();
  }

  // Use a vector when random access is needed
  std::vector<int> v{1, 2, 3};
  std::cout << '\n' << v[1] << '\n';  // prints 2
  for (int i : v) {
    std::cout << i << ' ';  // prints 1 2 3
  }
}
3 2 1
2
1 2 3

So in summary - use std::stack for LIFO access to the top element only, and std::vector for random access, iteration, or access to the last element. The best choice depends on your specific access and usage requirements.

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.

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?
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?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant