C++ stack

In computer science we go for working on a large variety of programs. Each of them has their own domain and utility. Based on the purpose and environment of the program creation, we have a large number of data structures available to choose from. One of them is 'stack'. Before discussing about this data type let us take a look at its syntax.


  1. template<class T, class Container = deque<T> > class stack;  

This data structure works on the LIFO technique, where LIFO stands for Last In First Out. The element which was first inserted will be extracted at the end and so on. There is an element called as 'top' which is the element at the upper most position. All the insertion and deletion operations are made at the top element itself in the stack.

Stacks in the application areas are implied as the container adaptors.

The containers should have a support for the following list of operations:

  • empty
  • size
  • back
  • push_back
  • pop_back

Template Parameters

T: The argument specifies the type of the element which the container adaptor will be holding.

Container: The argument specifies an internal object of container where the elements of the stack are hold.

Member Types

Given below is a list of the stack member types with a short description of the same.

Member Types Description
value_type Element type is specified.
container_type Underlying container type is specified.
size_type It specifies the size range of the elements.


With the help of functions, an object or variable can be played with in the field of programming. Stacks provide a large number of functions that can be used or embedded in the programs. A list of the same is given below:

Function Description
(constructor) The function is used for the construction of a stack container.
empty The function is used to test for the emptiness of a stack. If the stack is empty the function returns true else false.
size The function returns the size of the stack container, which is a measure of the number of elements stored in the stack.
top The function is used to access the top element of the stack. The element plays a very important role as all the insertion and deletion operations are performed at the top element.
push The function is used for the insertion of a new element at the top of the stack.
pop The function is used for the deletion of element, the element in the stack is deleted from the top.
emplace The function is used for insertion of new elements in the stack above the current top element.
swap The function is used for interchanging the contents of two containers in reference.
relational operators The non member function specifies the relational operators that are needed for the stacks.
uses allocator<stack> As the name suggests the non member function uses the allocator for the stacks.

Example: A simple program to show the use of basic stack functions.

  1. #include <iostream>  
  2. #include <stack>  
  3. using namespace std;  
  4. void newstack(stack <int> ss)  
  5. {  
  6.     stack <int> sg = ss;  
  7.     while (!sg.empty())  
  8.     {  
  9.         cout << '\t' << sg.top();  
  10.         sg.pop();  
  11.     }  
  12.     cout << '\n';  
  13. }  
  14. int main ()  
  15. {  
  16.     stack <int> newst;  
  17.     newst.push(55);  
  18.     newst.push(44);  
  19.     newst.push(33);  
  20.     newst.push(22);  
  21.     newst.push(11);  
  23.     cout << "The stack newst is : ";  
  24.     newstack(newst);  
  25.     cout << "\n newst.size() : " << newst.size();  
  26.     cout << "\n newst.top() : " << newst.top();  
  27.     cout << "\n newst.pop() : ";  
  28.     newst.pop();  
  29.     newstack(newst);   
  30.     return 0;  
  31. }  


The stack newst is : 	11	22	33	44	55

 newst.size() : 5
 newst.top() : 11
 newst.pop() : 	22	33	44	55
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #