Stack is a data structure designed to operate in LIFO (Last in First out) context. In stack elements are inserted as well as get removed from only one end.
Stack class is container adapter. Container is an objects that hold data of same type. Stack can be created from different sequence containers. If container is not provided it uses default deque container. Container adapters do not support iterators therefore we cannot use them for data manipulation. However they support push() and pop() member functions for data insertion and removal respectively.
Below is definition of std::stack from <stack> header file
template <class T, class Container = deque<T> > class stack;
T − Type of the element contained.
T may be substituted by any other data type including user-defined type.
Container − Type of the underlying container object.
Following member types can be used as parameters or return type by member functions.
Sr.No. | Member types | Definition |
---|---|---|
1 | value_type | T (First parameter of the template) |
2 | container_type | Second parameter of the template |
3 | size_type | size_t |
4 | reference | value_type& |
5 | const_reference | const value_type& |
Below is list of all methods from <stack> header.
Sr.No. | Method & Description |
---|---|
1 | stack::stack default constructor Constructs an empty stack object, with zero elements. |
2 | stack::stack copy constructor Constructs a stack with copy of each elements present in another stack. |
3 | stack::stack move constructor Constructs a stack with the contents of other using move semantics. |
Sr.No. | Method & Description |
---|---|
1 | stack::~stack
Destroys stack by deallocating container memory. |
Sr.No. | Method & Description |
---|---|
1 | stack::emplace
Constructs and inserts new element at the top of stack. |
2 | stack::empty
Tests whether stack is empty or not. |
3 | stack::operator= copy version Assigns new contents to the stack by replacing old ones. |
4 | stack::operator= move version Assigns new contents to the stack by replacing old ones. |
5 | stack::pop
Removes top element from the stack. |
6 | stack::push copy version Inserts new element at the top of the stack. |
7 | stack::push move version Inserts new element at the top of the stack. |
8 | stack::size
Returns the total number of elements present in the stack. |
9 | stack::swap
Exchanges the contents of stack with contents of another stack. |
10 | stack::top
Returns a reference to the topmost element of the stack. |
Sr.No. | Method & Description |
---|---|
1 | operator==
Tests whether two stacks are equal or not. |
2 | operator!=
Tests whether two stacks are equal or not. |
3 | operator<
Tests whether first stack is less than other or not. |
4 | operator<=
Tests whether first stack is less than or equal to other or not. |
5 | operator>
Tests whether first stack is greater than other or not. |
6 | operator>=
Tests whether first stack is greater than or equal to other or not. |
7 | swap
Exchanges the contents of two stack. |