How to Pop an Element From a Stack in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, stacks are used to store a collection of similar types of data in a Last-In-First-Out (LIFO) manner. In this article, we will discuss how to pop an element from a stack in C++. Example:Input: myStack = {10, 34, 12, 90, 1}; Output: myStack = {10, 34, 12, 90};Removing an Element from a Stack in C++In STL, the std::stack::pop() function is used to remove the top element from the stack. However, it’s important to note that this function does not return the popped element. To access the top element before popping it, we can use the std::stack::top() function. C++ Program to Pop an Element From a Stack C++ // C++ Program to pop an element from a stack #include <iostream> #include <stack> using namespace std; int main() { // Creating a stack with elements stack<int> stackData; // Pushing elements to the stack stackData.push(10); stackData.push(20); stackData.push(30); stackData.push(40); // Printing the original stack before popping cout << "Before Popping - Original Stack: "; stack<int> oStack = stackData; while (!oStack.empty()) { cout << oStack.top() << " "; oStack.pop(); } cout << endl; // Popping an element from the stack if (!stackData.empty()) { stackData.pop(); } // Printing the updated stack after popping cout << "After Popping - Updated Stack: "; while (!stackData.empty()) { cout << stackData.top() << " "; stackData.pop(); } cout << endl; return 0; } OutputBefore Popping - Original Stack: 40 30 20 10 After Popping - Updated Stack: 30 20 10 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Pop an Element From a Stack in C++? G gauravgandal Follow Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Push an Element into Stack in C++? In C++ Stacks are a type of container adaptor with LIFO(Last In First Out) type of work, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn to push the elements onto a Stack in C++. Example:Input:myStack = {40};Output:myStack: { 2 min read How to Access the Top Element of a Stack in C++? In C++, The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure in which insertion and deletion are done at the end only. In this article, we will learn how to access the top element of a stack in C++. Example: Input: myStack = {10, 20, 30, 40} Output: Top El 2 min read How to Dequeue an Element from a Queue in C++? In C++, queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. In this article, we will learn how to dequeue an element from a queue in C++ STL. Example: Input: Create a queue an 2 min read How to Clear a Stack in C++? In C++, clearing a stack means removing all element from the stack container leaving it empty. In this article, we will learn how to clear a stack in C++.The most efficient method to clear a stack is by assigning the new empty stack to our original stack container. Let's take a look at the code exam 3 min read How to Declare a Stack in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn how to declare a stack in C++. Declaring a Stack in C++ STLThe C++ STL provides a co 2 min read Like