Search code examples
c++stackcontainersstdvectorstddeque

Why use std::stack or std::queue?


Why would I use a std::stack or a std::queue rather than a std::vector or a std::deque?

Since the container adapters are merely wrappers around the standard containers, why use them at all?


Solution

  • To limit the user interface You don't want your stack to be able to remove elements somewhere else instead of top. Why to use vector in place of stack if you have exactly same performance also stack improves readability and reliability.

    std::stack is more expressive than std::vector when the container you want to implement is truly a LIFO.