C++ Stack::size() Function



The C++ function std::stack::size() returns the total number of elements present in the stack.

We can use the size() function to check whether the given stack is empty or not, as it will return 0 if the stack does not contain any element.

The size() function is a simple and efficient tool for managing stacks in C++. Knowing the size of the stack can be useful in many situations, such as allocating memory, copying elements to another stack or container, removing elements from a stack, or resizing data structures.

Syntax

Following is the syntax for std::stack::size() function −

stack_name.size();

Parameters

This function does not accept any parameter.

Return value

Returns the number of element present in the stack.

Example 1

The following example shows the usage of the std::stack::size() function. Here, we are creating an empty stack 's' and retrieving its size using the size() function.

#include <iostream>
#include <stack>
using namespace std;

int main() {
   stack<int> s;
   cout << "Initial size of stack = " << s.size() << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

Initial size of stack = 0

Example 2

Here, we are creating a stack 's'. Then we are inserting integer values from 1 to 5 into 's' using a for loop and the push() function. Thereafter, we are retrieving the size of the stack using the size() function.

#include <iostream>
#include <stack>
using namespace std;

int main() {
   stack<int> s;
   for (int i = 0; i < 5; ++i)
      s.push(i);
   cout << "Size of stack after adding 5 elements = " << s.size() << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

Size of stack after adding 5 elements = 5

Example 3

Now, we are trying to remove the elements from the stack and then printing its size before and after popping out all the elements.

#include <iostream>
#include <stack>
using namespace std;

int main() {
   stack<int> s;
   for (int i = 0; i < 5; ++i)
      s.push(i);
   cout << "Size of stack before popping elements = " << s.size() << endl;
   while (!s.empty())
      s.pop();
   cout << "Size of stack after popping elements = " << s.size() << endl;
   return 0;
}

Output

Following is an output of the above code −

Size of stack before popping elements = 5
Size of stack after popping elements = 0

Example 4

In the following example, we are creating two stacks 's1' and 's2'. We are then inserting elements into s1 and copying all the elements from 's1' to 's2'. Then we are retrieving the size of both the stacks using the size() function, demonstrating that 's2' is a copy of 's1'.

#include <iostream>
#include <stack>
using namespace std;

int main() {
   stack<int> s1;
   stack<int> s2;
   for (int i = 0; i < 5; ++i)
      s1.push(i);
   s2 = s1;
   cout << "Size of stack s1 = " << s1.size() << endl;
   cout << "Size of stack s2 (copy of s1) = " << s2.size() << endl;
   return 0;
}

Output

Output of the above code is as follows −

Size of stack s1 = 5
Size of stack s2 (copy of s1) = 5
Advertisements