Published Jul 29, 2022
Contribute to Docs
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
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;
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.
Looking to contribute?
- 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.
Learn C++ on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification