Signup/Sign In

C++ STL Stack Program

Hello Everyone!

In this tutorial, we will learn about the working of a Stack and its implementation in the C++ programming language.

To understand the basic functionality of the Stack, we will recommend you to visit the Stack Data Structure, where we have explained this concept in detail from scratch.

For a better understanding of its implementation, refer to the well-commented C++ code given below.


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

//Function to print the elements of the stack
void show(stack<int> s)
    while (!s.empty())
        cout << "  " <<; //printing the topmost element
        s.pop();                 //removing the topmost element to bring next element at the top

    cout << endl;

int main()
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the working of Stacks, in CPP  ===== \n\n";

    int i;

    //Stack declaration (stack of integers)
    stack<int> s;

    //Filling the elements by using the push() method.
    cout << "Filling the Stack in LIFO order:"; //LIFO= Last In First Out
    for (i = 1; i <= 5; i++)
        s.push(i * 10); //inserting elements to the top

    cout << "\n\nThe top-most element of the Stack is :  " <<;

    cout << "\n\nThe size of the Stack is :  " << s.size();

    cout << "\n\nThe elements of the Stack in LIFO order are: ";

    cout << "\n\nAfter deleting the top-most element from the stack, it becomes: ";

    cout << "\n\n\n";

    return 0;


C++ Stack Program

We hope that this post helped you develop a better understanding of the concept of Stack and its implementation in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )

About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.