C++ STL Stack Program
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.
using namespace std;
//Function to print the elements of the stack
void show(stack<int> s)
cout << " " << s.top(); //printing the topmost element
s.pop(); //removing the topmost element to bring next element at the top
cout << endl;
cout << "\n\nWelcome to Studytonight :-)\n\n\n";
cout << " ===== Program to demonstrate the working of Stacks, in CPP ===== \n\n";
//Stack declaration (stack of integers)
//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 : " << s.top();
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";
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 : )