Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::stack considered useless?

Tags:

c++

stack

Numerous answers and comments on SO claim that using std::stack is pretty much useless, but don't really give a reason other than "Use a std::vector or std::deque instead."

Why? What's wrong with it?

Evidence of Claims:

  • Answer claiming using vector or deque is better.
  • Answer claiming just use straight deque for iteration (see also comments).
  • Answer directly claiming stack is useless.
  • Answer claims vector is just as usable in addition to accessing inner elements
like image 881
Casey Avatar asked Jul 19 '26 13:07

Casey


1 Answers

There is a saying that goes like this:

Don't tempt someone to do something you don't want him/her to do.

A stack is a well-defined data structure. It's behaviour is well implemented by std::stack.

If all you need is a stack and nothing more, then go for std::stack. It will help you stick to the purpose of the data structure, plus make your program clearer to understand and easier to maintain.

But try to evaluate whether you will ever need more functionality than a stack provides.

For example, a good reason to argue in favour of std::deque or std::vector is iterators, as already stated.


In all cases provided as evidence for the fact that using std::stack is pretty much useless, it was such additional functionality (beyond that of a stack) which motivated abandoning std::stack in favour of another container.

like image 57
lamonadevide Avatar answered Jul 21 '26 03:07

lamonadevide