
- 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::empty() Function
The C++ std::stack::empty() function checks whether the stack is empty or not (having no elements). A stack of size zero is considered an empty stack.
This function does not accept any parameter, and it is a const member function. The "const" keyword indicates that the function does not modify the stack. It returns a boolean value true if the stack is empty, and false otherwise.
When a stack is empty, attempting to pop an element from it will result in an error because there are no elements to remove. Therefore, empty stacks are commonly used as a starting point for operations that involve pushing elements.
Syntax
Following is the syntax for std::stack::empty() function −
bool stack_name.empty() const;
Parameters
This method does not accept any parameter.
Return value
Returns true if the stack is empty, otherwise false.
Example 1
The following example shows the usage of the std::stack::empty() function. At first, we are trying to create a stack s with no elements in it and we verify it using the empty() function. Then, we insert/push element '1' into the stack and use the empty() function to determine if this stack is empty.
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s; if (s.empty()){ cout << "Stack is empty." << endl;} else { cout << "Stack is not empty." << endl;} s.emplace(1); if (!s.empty()){ cout << "Stack is not empty." << endl;} else { cout << "Stack is empty." << endl;} return 0; }
Output
Let us compile and run the above program, this will produce the following result −
Stack is empty. Stack is not empty.
Example 2
Here, we are trying to insert elements into the stack in a sequence of numbers from 1 to 6. Then, we use the pop() function to remove the elements one by one and calculate the product of the elements until the stack becomes empty.
#include <iostream> #include <stack> using namespace std; int main() { stack<int> s; // initializing the variable Product with the value 1 int Product = 1; for (int i=1;i<=6;i++) s.push(i); while (!s.empty()){ // multiplying the topmost element of the stack with Product Product = Product * s.top(); s.pop(); } cout << "The Product of elements is: " << Product << '\n'; return 0; }
Output
If we run the above code it will generate the following output −
The Product of elements is: 720
Example 3
In the following example, we create an empty stack s and then push the values '10', '20', '30', '3', and '0' into it. We then use the empty() function to check if the stack is empty or not, and print the result accordingly.
#include <iostream> #include <stack> using namespace std; int main() { stack<int> s; s.push(10); s.push(20); s.push(30); s.push(3); s.push(0); cout<<"stack s is empty: \n"<<s.empty()<<"\n"; //empty stack stack<int> s1; cout<<"stack s1 is empty: \n"<<s1.empty(); }
Output
Folowing is an output of the above code −
stack s is empty: 0 stack s1 is empty: 1