(Translated by https://www.hiragana.jp/)
Stack in C++ STL - GeeksforGeeks
Open In App

Stack in C++ STL

Last Updated : 21 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Stack container follows LIFO (Last In First Out) order of insertion and deletion. It means that most recently inserted element is removed first and the first inserted element will be removed last. This is done by inserting and deleting elements at only one end of the stack which is generally called the top of the stack.

Syntax

Stack is defined as std::stack class template inside the <stack> header file.

stack<T> st;

where,

  • T: DataType (int, char etc.) of elements in the stack.
  • st: Name assigned to the stack.

Declaration and Initialization

#include <iostream>
#include <stack>
using namespace std;
int main(){
    stack<int>st;
    st.push(10);
    st.push(5);
    //Accessing top element
    cout<<"Top element: "<<st.top()<<endl;
    //Popping an element
    st.pop();
    cout<<"Top element after pop: "<<st.top()<<endl;
    return 0;
}

Output
Top element: 5
Top element after pop: 10

Basic Operations

Here are the basic operations that can be performed on a stack:

1. Inserting Elements

In stack, new elements can only be inserted at the top of the stack by using push() method.

Example:

#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<int> st;
    
    // Inserting element top of the stack
    st.push(10);
    st.push(20);
    st.push(30);
    st.push(40);
    return 0;
}
Push-Operation-in-Stack-(1)

2. Accessing Elements

Only the top element of the stack can be accessed using top() method.

Example:

#include <bits/stdc++.h>
using namespace std;

int main() {
    stack<int> st;
    st.push(10);
    st.push(20);
    st.push(30);
    st.push(40);

    // Accessing the top element
    cout << st.top();
    return 0;
}

Output
40
Top-or-Peek-Operation-in-Stack-(1)

Note: Peek() is used to only view the topmost element of the stack not print it.

3. Deleting Elements

In stack, only the top element of the stack can be deleted by using pop() method in one operation.

Example:

#include <bits/stdc++.h>
using namespace std;

int main() {
    stack<int> st;
    st.push(10);
    st.push(20);
    st.push(30);
    st.push(40);
    
    // Deleting top element
    st.pop();
    
    while(!st.empty()) {
        cout << st.top() << " ";
        st.pop();
    }
    return 0;
}

Output
30 20 10 
Pop-Operation-in-Stack-(1)

4.empty()

This checks whether the stack is empty. It returns true if the stack has no elements; otherwise, it returns false.

#include <iostream>
#include <stack>
using namespace std;

int main() {

    stack<int>st;
    if(st.empty()){
        cout<<"Stack is empty "<<endl;
    }
    st.push(100);
    if(!st.empty()){
        cout<<"Stack is not empty. Top element: "<<st.top()<<endl;
    }
    return 0;
}

Output
Stack is empty 
Stack is not empty. Top element: 100

5. Size of stack

The size() function in a stack returns the number of elements currently in the stack. It helps to determine how many items are stored without modifying the stack.

#include <iostream>
#include <stack>
using namespace std;

int main() {

    stack<int>st;
    st.push(10);
    st.push(5);
    cout<<"Size of stack: "<<st.size()<<endl;
    st.pop();
    cout<<"Size of stack:"<<st.size()<<endl;
    return 0;
}

Output
Size of stack: 2
Size of stack:1

Pseudo Traversal

As any element other than the top element cannot be accessed in the stack, it cannot be actually traversed. But we can create a copy of it, access the top element and delete the top. By doing this till the copy stack is empty, we can effectively traverse without modifying the original stack.

Example:

#include <bits/stdc++.h>
using namespace std;

int main() {
    stack<int> st;
    st.push(10);
    st.push(20);
    st.push(30);
    st.push(40);
    
    // Create a copy
    stack<int> temp(st);
    
    while(!temp.empty()) {
        cout << temp.top() << " ";
        temp.pop();
    }
    return 0;
}

Output
40 30 20 10 

Explanation: In the above program, pseudo-traversal is done by creating a copy of the st to avoid modifying the original stack. The while loop prints and removes elements from temp using top() and pop() till the stack is not empty.

Time Complexity

The below table lists the time complexity of the above operations on stack:

OperationTime Complexity
Insert an element (push)O(1)
Delete an element (pop)O(1)
Access top element (peek)O(1)
Traverse the stackO(n)

Stack in C++ STL
Visit Course explore course icon

Similar Reads