Skip to content
Related Articles

Related Articles

Stack in C++ STL
  • Difficulty Level : Easy
  • Last Updated : 18 Feb, 2021

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.  Stack uses an encapsulated object of either vector or deque (by default) or list (sequential container class) as its underlying container, providing a specific set of member functions to access its elements.
The functions associated with stack are: 
empty() – Returns whether the stack is empty – Time Complexity : O(1) 
size() – Returns the size of the stack – Time Complexity : O(1) 
top() – Returns a reference to the top most element of the stack – Time Complexity : O(1) 
push(g) – Adds the element ‘g’ at the top of the stack – Time Complexity : O(1) 
pop() – Deletes the top most element of the stack – Time Complexity : O(1) 


// CPP program to demonstrate working of STL stack
#include <bits/stdc++.h>
using namespace std;
void showstack(stack <int> s)
    while (!s.empty())
        cout << '\t' <<;
    cout << '\n';
int main ()
    stack <int> s;
    cout << "The stack is : ";
    cout << "\ns.size() : " << s.size();
    cout << "\ : " <<;
    cout << "\ns.pop() : ";
    return 0;


The stack is :     1    5    20    30    10

s.size() : 5 : 1
s.pop() :     5    20    30    10


List of functions of Stack: 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :