In C++, the Standard Template Library (STL) offers powerful and efficient container classes to organize data collections. A stack is a container that is based on the LIFO (Last In, First Out) structure. It indicates that the last element added will be the first one to be removed. It provides a certain interface that is built over another container, such as a deque, vector, or list. In a stack, we only have one access point at the end of the stack, which is referred to as the top, where we perform insertion and deletion operations.

In C++, the Stack size() function returns the number of stack elements. The number of stack elements is referred to as the size of the stack. The size of the stack elements helps to indicate how many elements are present in the stack, which can help us deduce many other things, such as the memory and space required.
It has the following syntax:
In this syntax;
Parameters: It has no parameters. It simply gives the size of the stack under reference. Since the function is used to get an idea about the stack size, there is no purpose for the argument in the program.
Return value: The number of elements in the stack is returned, which is a measure of the size of the stack. Hence, the function has an integer return type as size is an int value.
Here, we are going to discuss several examples to demonstrate the stack size() function.
This example demonstrates how to use the size() function to find the number of elements present in a stack.
Output:
0. size: 0 1. size: 5 2. size: 4
Explanation:
In this example, we have created a stack of integers named tpoint to demonstrate the size() function in C++. Initially, the stack is empty, so its size is 0. After that, we use the push() function to insert five elements (0 to 4) onto the stack, and then the size of the stack becomes 5. Next, we use the pop() function to remove one element, and then the size of the stack decreases to 4. Finally, it shows the stack's size dynamically changes as elements are added or removed.
This example demonstrates how to insert elements into a stack and display its size using the size() function.
Output:
3
Explanation:
In this example, we have created a stack of integers named tpoint that keeps three elements (28, 37, and 55) that were pushed onto it. After that, we use the size() function to get the total number of elements currently present in the stack. Finally, the three elements were added, and the output displays 3.
This example demonstrates how to compare the sizes of two stacks using the size() function.
Output:
Size of a: 3 Size of b: 2
Explanation:
In this example, we have created two integer stacks, a and b, that contain three elements (5, 8, 50), while stack b has two elements (132, 45). After that, we use the size() function to display the number of elements in each stack. Finally, the output shows that a has a size of 3 and b has a size of 2.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India