The C++ Standard Library
- C++ Library - <valarray>
- C++ Library - <utility>
- C++ Library - <typeinfo>
- C++ Library - <tuple>
- C++ Library - <thread>
- C++ Library - <string>
- C++ Library - <stdexcept>
- C++ Library - <regex>
- C++ Library - <numeric>
- C++ Library - <new>
- C++ Library - <memory>
- C++ Library - <locale>
- C++ Library - <limits>
- C++ Library - <functional>
- C++ Library - <exception>
- C++ Library - <complex>
- C++ Library - <atomic>
- C++ Library - <streambuf>
- C++ Library - <sstream>
- C++ Library - <ostream>
- C++ Library - <istream>
- C++ Library - <iostream>
- C++ Library - <iosfwd>
- C++ Library - <ios>
- C++ Library - <iomanip>
- C++ Library - <fstream>
- C++ Library - Home
The C++ STL Library
- C++ Library - <iterator>
- C++ Library - <algorithm>
- C++ Library - <vector>
- C++ Library - <unordered_set>
- C++ Library - <unordered_map>
- C++ Library - <stack>
- C++ Library - <set>
- C++ Library - <queue>
- C++ Library - <map>
- C++ Library - <list>
- C++ Library - <forward_list>
- C++ Library - <deque>
- C++ Library - <bitset>
- C++ Library - <array>
C++ Programming Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
C++ Library - <stack>
Introduction
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.
Definition
Below is definition of std::stack from <stack> header file
template <class T, class Container = deque<T> > class stack;
Parameters
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.
Member types
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& |
Functions from <stack>
Below is pst of all methods from <stack> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs an empty stack object, with zero elements. |
default constructor
2 | Constructs a stack with copy of each elements present in another stack. |
copy constructor
3 | Constructs a stack with the contents of other using move semantics. |
move constructor
Destructor
Sr.No. | Method & Description |
---|---|
1 | Destroys stack by deallocating container memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Constructs and inserts new element at the top of stack. |
2 | Tests whether stack is empty or not. |
3 | Assigns new contents to the stack by replacing old ones. |
copy version
4 | Assigns new contents to the stack by replacing old ones. |
move version
5 | Removes top element from the stack. |
6 | Inserts new element at the top of the stack. |
copy version
7 | Inserts new element at the top of the stack. |
move version
8 | Returns the total number of elements present in the stack. |
9 | Exchanges the contents of stack with contents of another stack. |
10 | Returns a reference to the topmost element of the stack. |
Non-member overloaded functions
Sr.No. | Method & Description |
---|---|
1 | Tests whether two stacks are equal or not. |
2 | Tests whether two stacks are equal or not. |
3 | Tests whether first stack is less than other or not. |
4 | Tests whether first stack is less than or equal to other or not. |
5 | Tests whether first stack is greater than other or not. |
6 | Tests whether first stack is greater than or equal to other or not. |
7 | Exchanges the contents of two stack. |