size() in Stack C++ STL

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Stack is a data structure which works on the principle of Last in First Out (LIFO) i.e. the last element that is inserted into the stack is the first one to be removed. The Standard Template Library (STL) of C++ provides stack template class which can be used to implement stack data structure. The stack template class contains various fuctions which can be used to perform operations on the stack. In this article we will be discussing about size() function of stack in C++ STL.

Read about Stack in C++ STL

What is size of a stack?

Size of a stack is the number of items present in a stack at a particular instant.

Consider the below stack. It contains four items i.e. 5, 7, 4, 10. Therefore the size of the stack is 4.


Now let's consider an empty stack. The empty stack does not have any items and it's size is 0.

stack::size()

This function is present in the template class of stack in C++ STL. It returns the number of elements in the stack. This member function effectively calls member size of the underlying container object.

Parameters
None

Return value
It returns number of elements in the underlying container. The value returned is of type unsigned integer.

Time Complexity
O(1)

Syntax

stackname.size()

Example

stack<int> container;
int size_of_stack = container.size();

In the above example, container is the stack and size() method is called on it which returns the size of the stack i.e. zero. Therefore size_of_stack contains the value 0.

Example code for stack::size() function

// Part of OpenGenus
#include <iostream>
#include <stack>

using namespace std;

int main() {
    stack<int> container;

    // print stack size
    cout<<"Initial size of stack: "<<container.size()<<"\n";

    // Add three elements to stack using push()
    container.push(7);
    container.push(3);
    container.push(11);

    // print stack size
    cout<<"Size of stack after adding three elements: "<<container.size()<<"\n";

    // Remove two elements from stack
    container.pop();
    container.pop();

    // print stack size
    cout<<"Size of stack after removing two elements: "<<container.size()<<"\n";

    return 0;
}

Output

Initial size of stack: 0
Size of stack after adding three elements: 3
Size of stack after removing two elements: 1

From the above code, we can say that when the stack is first created, it's size is 0 and it is empty. When three elements are added to the stack using stack::push() function the stack size is 3 because there are three elements present in the stack. When two elements are removed from the stack using stack::pop() function, the stack size is 1 as only one element is present in the stack.

Common applications of stack::size()

  1. To get number of elements present inside the stack: stack::size() returns the number of elements present inside the stack.
  2. To loop until the stack is empty: If you want to perform some operation on all elements present in the stack, then you can use a while loop to get elements inside a stack as long as the stack size is not equal to zero.

Example code:

#include <iostream>
#include <stack>

using namespace std;

int main() {
    stack<int> container;

    // Add elements to stack using push()
    container.push(7);
    container.push(3);
    container.push(11);
    container.push(5);
    container.push(2);

    // loop until the size of stack is not zero
    while(container.size() != 0) {
        // obtain the top element
        int element = container.top();
        // remove top element from stack
        container.pop();

        cout<<element<<" ";
    }

    return 0;
}

Output

2 5 11 3 7

Here we are looping through elements in the stack until it's empty. Each time we perform a stack::pop() operation on the stack, top element from the stack is removed and the size of the stack decreases by one. When no elements are left in the stack, the size is equal to 0 and we exit the loop.

  1. To check whether the stack is not empty before stack::pop() operation: If you try to perform stack:pop() operation on an empty stack, 'Segmentation fault' occurs. Segmentation faults are caused by a program trying to read or write an illegal memory location. Therefore, it is necessary to check whether the stack size is greater than 0 before trying to perform stack::pop() operation.

Example code:

if(stack.size()>0)
    container.pop();
else
    cout<<"The stack is empty.\n";

Now we will go through some questions to solidify your understanding.

Question 1

Which of the below statements is used to get the size of 'container' stack?

container.size()
size(container)
container.top()
container(size)
stackname.size() is the correct syntax to get stack size.

Question 2

What is the size of the stack after these sequence of operations - push(3)-> push(2)-> pop()-> pop()-> push(4)->push(7)->push(10)->pop()?

3
4
5
2
After pushing(adding) 2 elements, 2 elements are popped(removed) from stack. Stack size is now 0. Now, 3 elements are pushed and one element is popped from the stack. Now, final stack size is 2.

With this article at OpenGenus, you should have a complete idea of size() function in stack container of C++ Standard Template Library (STL).

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.