A stack is a linear dynamic data structure that follows Last-In/First-Out (LIFO) principle. In a stack, addition of a new element and deletion of an element occurs at the same end which implies that the element which is added last in the stack will be the first to be removed from the stack.
Features of stack
- It is a dynamic data structure.
- It has dynamic size.
- It uses dynamic memory allocation.
Syntax
template < class T, class Container = deque<T> > class stack;
Parameters
T |
Type of the elements stored in the container. |
Container |
Type of the underlying container object where the elements are stored. |
Member Types
Member types | Definition |
value_type | T (First template parameter) |
container_type | Container (Second template parameter), deque<value_type> |
reference | value_type& |
const_reference | const value_type& |
size_type | size_t |
The C++ stack container has a number of member functions which are listed below:
Functions | Description |
empty() |
Checks whether the stack is empty or not. |
size() |
Returns the length of the stack in terms of bytes. |
top() |
Access top element of the stack. |
push() |
Adds a new element at the top of the stack. |
pop() |
Deletes top element of the stack. |
emplace() |
Constructs and inserts a new element at the top of the stack. |
swap() |
Exchanges elements between two stacks. |
Functions | Description |
operator == |
Checks whether two stacks are equal or not. |
operator != |
Checks whether two stacks are unequal or not. |
operator < |
Checks whether the first stack is less than the other or not. |
operator > |
Checks whether the first stack is greater than the other or not. |
operator <= |
Checks whether the first stack is less than or equal to the other or not. |
operator >= |
Checks whether the first stack is greater than or equal to the other or not. |
swap() |
Exchanges elements between two stacks. |