Home > Net >  Does the stack implementation that's part of the C STL have a capacity?
Does the stack implementation that's part of the C STL have a capacity?

Time:06-23

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?

CodePudding user response:

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.)

CodePudding user response:

stacks in STL are a type of container adapter, so whether to define the capacity will depend on the type of underlying container you have used(vector, deque(default), or list). one more thing, this should not be confused with the stack memory, which has a limited size.

  • Related