Search code examples
c++vectordata-structuresstackcontainers

Why do we need stacks when we already have vectors which are even more powerful?


In C++ STL, Stacks are implemented using container adaptors which rewrite the interface of the Vector class. However, why is it necessary to do the interface rewriting and design a Stack class when there is already the Vector class available? Is it due to cost efficiency i.e. maintaining a stack uses less resources while it could do all necessary jobs?


Solution

  • Why do we need for loops and while loops when we already have goto which is even more powerful? You should adhere to the principle of parsimony - use the least powerful tool that is powerful enough to achieve the desired objective.

    If what you need is a stack, take a dependency on the standard library class that provides that functionality, not a more powerful one. It also communicates better to a person reading your code, what you are going to do.