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:
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:
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.
Let's say std::stack is not useless but it is only a reasonable choice if one of the following is true:
Basically std::stack's lack of any kind of iterator interface means that it can't be used with any of the standard algorithms, if this is a problem to you then use a vector you treat as a stack or roll your own stack template.