Search code examples
c++data-structuresstlstack

Does the stack implementation that's part of the C++ STL have a capacity?


I learned in my college DSA course that a stack is initialized with a capacity that limits the number of elements it can contain. But when I create a stack using the STL, you don't have to define a capacity. Is there a capacity involved, or does it not apply in the STL implementation? Do stacks even really need a capacity?


Solution

  • The stack implementation you looked at in your course may have had a limit, but that's not essential to being a stack. (And your course really should have taught you this.)

    The C++ standard library stack is just an adapter for any underlying collection that supports the necessary operations, so whether it has a limited capacity or not depends on that underlying type.
    (The default is std::deque.)