Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.
empty() function is used to check if the stack container is empty or not.
Syntax :
stackname.empty() Parameters : No parameters are passed. Returns : True, if stack is empty False, Otherwise
Examples:
Input : mystack
mystack.empty();
Output : True
Input : mystack = 1, 2, 3
Output : False
Errors and Exceptions
1. Shows error if parameter is passed
2. Shows no exception throw guarantee.
// CPP program to illustrate // Implementation of empty() function #include <iostream> #include <stack> using namespace std; int main() { stack<int> mystack; mystack.push(1); // Stack becomes 1 if (mystack.empty()) { cout << "True"; } else { cout << "False"; } return 0; } |
Output:
False
Application :
Given a stack of integers, find the sum of the all the integers.
Input : 1, 8, 3, 6, 2 Output: 20
Algorithm
1. Check if the stack is empty, if not add the top element to a variable initialised as 0, and pop the top element.
2. Repeat this step until the stack is empty.
3. Print the final value of the variable.
// CPP program to illustrate // Application of empty() function #include <iostream> #include <stack> using namespace std; int main() { int sum = 0; stack<int> mystack; mystack.push(1); mystack.push(8); mystack.push(3); mystack.push(6); mystack.push(2); // Stack becomes 1, 8, 3, 6, 2 while (!mystack.empty()) { sum = sum + mystack.top(); mystack.pop(); } cout << sum; return 0; } |
Output:
20
size() function is used to return the size of the stack container or the number of elements in the stack container.
Syntax :
stackname.size() Parameters : No parameters are passed. Returns : Number of elements in the container.
Examples:
Input : mystack = 0, 1, 2
mystack.size();
Output : 3
Input : mystack = 0, 1, 2, 3, 4, 5
mystack.size();
Output : 6
Errors and Exceptions
1. Shows error if a parameter is passed.
2. Shows no exception throw guarantee.
// CPP program to illustrate // Implementation of size() function #include <iostream> #include <stack> using namespace std; int main() { int sum = 0; stack<int> mystack; mystack.push(1); mystack.push(8); mystack.push(3); mystack.push(6); mystack.push(2); // Stack becomes 1, 8, 3, 6, 2 cout << mystack.size(); return 0; } |
Output:
5
Application :
Given a stack of integers, find the sum of the all the integers.
Input : 1, 8, 3, 6, 2 Output: 20
Algorithm
1. Check if the size of the stack is zero, if not add the top element to a variable initialised as 0, and pop the top element.
2. Repeat this step until the stack size becomes 0.
3. Print the final value of the variable.
// CPP program to illustrate // Application of size() function #include <iostream> #include <stack> using namespace std; int main() { int sum = 0; stack<int> mystack; mystack.push(1); mystack.push(8); mystack.push(3); mystack.push(6); mystack.push(2); // Stack becomes 1, 8, 3, 6, 2 while (mystack.size() > 0) { sum = sum + mystack.top(); mystack.pop(); } cout << sum; return 0; } |
Output:
20
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- list::empty() and list::size() in C++ STL
- queue::empty() and queue::size() in C++ STL
- deque::empty() and deque::size() in C++ STL
- priority_queue::empty() and priority_queue::size() in C++ STL
- vector::empty() and vector::size() in C++ STL
- forward_list::front() and forward_list::empty() in C++ STL
- array::empty() in C++ STL
- map::empty() in C++ STL
- set::empty() in C++ STL
- list empty() function in C++ STL
- unordered_multiset empty() function in C++STL
- unordered_multimap empty() function in C++ STL
- match_results empty() in C++ STL
- unordered_set empty() function in C++ STL
- multimap empty() function in C++ STL
- unordered_map empty in C++ STL
- multiset empty() function in C++ STL
- Why is the size of an empty class not zero in C++?
- stack push() and pop() in C++ STL
- Stack in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

