How to Clear a Stack in C++? Last Updated : 15 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 example: C++ #include <bits/stdc++.h> using namespace std; int main() { stack<int> s; s.push(1); s.push(5); s.push(4); // Initial size of the stack cout << s.size() << endl; // Assigning the new empty stack s = stack<int>(); // Final size of the stack cout << s.size() << endl; return 0; } Output3 0 This method destroys the previously stored elements.There are also a few other methods to clear a stack in C++. They are given below:Table of ContentUsing Stack swap() MethodUsing Stack pop() MethodUsing Stack swap() MethodA new empty stack container is swapped with original std::stack container by using stack swap() function. This method is similar to the previous method but allows us to save the stack elements before clearing it. C++ #include <bits/stdc++.h> using namespace std; int main() { stack<int> s; s.push(1); s.push(5); s.push(4); // Initial size of the stack cout << s.size() << endl; // Swap the stack with new empty stack stack<int> s1; s.swap(s1); // Final size of the stack cout << s.size() << endl; return 0; } Output3 0 Using Stack pop() MethodThe stack pop() method removes the top element of the stack. It can be used to clear the stack by popping all the elements one by one in a loop until the stack is empty. C++ #include <bits/stdc++.h> using namespace std; int main() { stack<int> s; s.push(1); s.push(5); s.push(4); // Initial size of the stack cout << s.size() << endl; // Removing all elements from stack one by one while (!s.empty()) s.pop(); // Final size of the stack cout << s.size() << endl; return 0; } OutputStack is EmptyExplanation: In the above code, we remove all elements from stack container one by one till stack empty() does not returns true. Comment More infoAdvertise with us Next Article How to Clear a Stack in C++? A anjalibo6rb0 Follow Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 How to Copy a Stack in C++? In C++, stacks are a type of container adaptor with a 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 are going to discuss how to copy a stack in C++. Copying a Stack to Another in C++To copy a s 2 min read How to Create a Stack of Set in C++? In C++ STL, 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. Sets are a type of associative container in which each element is unique and in some sorted order. In this arti 2 min read How to Create a Stack of Stack in C++? In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in 2 min read How to Create a Stack of Lists in C++? In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Out 2 min read Like