Stacks
Published Jul 29, 2022
Contribute to Docs
A stack
is a container that stores elements in a last-in first-out (LIFO) order. They are implemented as a container adaptor, a class that uses another container class as its underlying container. The underlying container class can be a vector
, deque
, or a list
. If none is specified when creating a stack
instance, the default deque
is used.
Syntax
std::stack<dataType> stackName;
std::stack<dataType, containerType<dataType>> stackName;
A stack
requires a dataType
to be specified. A containerType
can be specified, but it must be the same data type. If a container type is not specified, the default deque<dataType>
is used.
Stacks
- .empty()
- Returns true if the stack has no elements.
- .pop()
- Removes the last item added to the top of the stack.
- .push()
- Adds an element to the top of the stack.
- .size()
- Returns the number of elements in the stack.
- .top()
- Returns the element on the top of the stack.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.