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.


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.


Returns true if the stack has no elements.
Removes the last item added to the top of the stack.
Adds an element to the top of the stack.
Returns the number of elements in the stack.
Returns the element on the top of the stack.

All contributors

Looking to contribute?

Learn C++ on Codecademy