Easy learning with example program codes

CPP Stacks

Stack is a data structure in CPP that works on LIFO (Last In First Out) technique, which means that the last inserted element will be deleted first in a stack.



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


  • T:

It is used to represent the type of the element that the container adapter will store.

  • Container:

It is used to represent the internal object of the container where the stack elements are stored.


Member Types of CPP Stack:

container_type To specify underlying container type.
size_type To specify the size range of the elements.
value_type To specify the element type.


CPP Stack Functions:

(constructor) To construct a stack container.
empty To test for the emptiness of a stack and to get a true value if the stack is empty.
emplace To insert a new element in the stack above the current top element.
push To insert a new element at the top of the stack.
pop To delete the element in the stack from the top.
swap To interchange the contents of two containers in reference.
size To return the size of the stack container, which is a measure of the number of elements stored in the stack.
top To access the top element of the stack.



#include <iostream>  
#include <stack>  
using namespace std;  
void display(stack <int> ds)  
stack <int> dis = ds;  
while (!dis.empty())  
	cout << << " ";  
int main()  
stack <int> getstack;  
cout << "The stack element from top to bottom are  : ";  
cout << "\nThe value of getstack.size() : " << getstack.size();  
cout << "\nThe value of : " <<;  
cout << "\nThe value of getstack.pop() : ";  
return 0;  


The stack element from top to bottom are  : 30 50 60 80    
The value of getstack.size() : 4                
The value of : 30                                 
The value of getstack.pop() : 50 60 80
Posted in CPP   
Sign Up/ Sign In
Ask a Question

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2018 CodesJava Protection Status SiteMap Reference: Java Wiki