
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Stack::pop() Function
The C++ function std::stack::pop() removes an element from the top of the stack, effectively decreasing its size by one.
The pop() function does not accept any parameter and returns void. Additionally, it's important to ensure that the pop() function cannot be called on an empty stack, if we try to do so it will throw an exception.
The pop() function acts as a deletion operation in LIFO (Last-In-First-Out) order. This implies that the last element inserted into the stack is the first one to be removed, and the first element inserted into the stack is the last one to be removed.
The pop() function has a complexity of O(1), which implies that it takes constant time to execute regardless of the size of the stack.
Syntax
Following is the syntax for std::stack::pop() function −
void stack_name.pop();
Parameters
This function does not accept any parameter.
Return value
This function has no return value.
Example 1
The following example shows the usage of the std::stack::pop() function. Here we are creating an empty stack of integers and inserting five elements into it using the emplace() function. Then we are retrieving and removing the elements in LIFO order using the top() and pop() functions respectively until the stack becomes empty.
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s; for (int i = 0; i < 5; ++i) s.emplace(i + 1); while (!s.empty()) { cout << s.top() << endl; s.pop(); } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
5 4 3 2 1
Example 2
Here, we are trying to insert elements into the stack in a sequence of numbers from 1 to 6. Then, we are using the pop() function to remove the elements one by one and calculating the product of the elements until the stack becomes empty. Finally, we are retrieving the size of the stack at each iteration of the while loop using the size() function and printing the product once the stack becomes empty.
#include <iostream> #include <stack> using namespace std; int main(){ stack<int> stck; int Product = 1; stck.push(1); stck.push(2); stck.push(3); stck.push(4); stck.push(5); stck.push(6); while (!stck.empty()){ Product = Product * stck.top(); cout<<"\nsize of stack is: "<<stck.size(); stck.pop(); } cout<<"\nThe product of the elements is: "<<Product; return 0; }
Output
If we run the above code it will generate the following output −
size of stack is: 6 size of stack is: 5 size of stack is: 4 size of stack is: 3 size of stack is: 2 size of stack is: 1 The product of the elements is: 720
Example 3
Now, we are creating an empty stack of character data type with the name s1 and inserting the characters 't', 'r', 'u', 'c', and 'k' into the stack in that order. Then we are retrieving and removing the elements from the stack using the top() and pop() functions respectively until the stack becomes empty.
#include <iostream> #include <stack> using namespace std; int main(void) { stack <char> s1; s1.push('t'); s1.push('r'); s1.push('u'); s1.push('c'); s1.push('k'); cout << "Contents of stack s1" << endl; while (!s1.empty()) { cout << s1.top() << endl; s1.pop(); } return 0; }
Output
Output of the above code is as follows
Contents of stack s1 k c u r t
Example 4
In the following example, we are creating a stack of float data type and then inserting 6 elements into it. Then we are removing the top 3 elements and then retrieving and removing the remaining elements until the stack becomes empty.
#include <iostream> #include <stack> using namespace std; int main() { stack<float> s; s.push(87.45f); s.push(43.76f); s.push(365.43f); s.push(32.67f); s.push(79.6f); s.push(645.76f); s.pop(); s.pop(); s.pop(); while (!s.empty()) { cout << s.top() << endl; s.pop(); } return 0; }
Output
Following is an output of the above code −
365.43 43.76 87.45