Stacks are a type of container adaptors, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from the end of the container.
s are implemented as containers adaptors
, which are classes that use an encapsulated object of a specific container class as its underlying container
, providing a specific set of member functions to access its elements. Elements are pushed
from the "back"
of the specific container, which is known as the top
of the stack.
The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it supports the following operations:
Therefore, the standard container class templates vector
can be used. By default, if no container class is specified for a particular stack
class, the standard container class template deque
In their implementation in the C++ Standard Template Library, stacks take two template parameters:
template < class T, class Container = deque<T> > class stack;
Where the template parameters have the following meanings:
- T: Type of the elements.
- Container: Type of the underlying container object used to store and access the elements.
In the reference for the stack member functions, these same names are assumed for the template parameters.
|empty|| Test whether container is empty (public member function)|
|size|| Return size (public member function)|
|top|| Access next element (public member function)|
|push|| Add element (public member function)|
|pop|| Remove element (public member function)|