C++ Stack Size Function - C++ Programming Tutorial
C++ Course / Data Structures / C++ Stack Size Function

C++ Stack Size Function

BLUF: Mastering C++ Stack Size Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Stack Size Function

C++ is renowned for its efficiency. Learn how C++ Stack Size Function enables low-level control and high-performance computing in the tutorial below.

In C++, the Standard Template Library (STL) provides robust and effective container classes for managing data collections. A stack is a container that follows the LIFO (Last In, First Out) principle, meaning the most recently added element is the first to be removed. It presents a specific interface constructed on top of another container like a deque, vector, or list. In a stack, there is a single access point located at the top of the stack, where insertions and deletions are carried out.

In C++, the size method in Stack class provides the count of elements in the stack. This count, known as the stack size, gives us insight into the quantity of elements within the stack. Understanding the stack's size enables us to make inferences about various aspects like memory usage and storage requirements.

Syntax:

It has the following syntax:

Example

size_type size() const;

In this syntax;

Parameters: This function does not require any parameters. Its sole purpose is to provide the size of the stack being referenced. As the function is designed to solely retrieve information about the stack size, there is no need for any arguments within the program.

The function returns the count of elements in the stack, indicating the stack's size. Therefore, the return type of the function is an integer as it corresponds to the integer value of the size.

C++ Simple Stack Size Function Example using Loops

In this example, we are using a specific case to demonstrate the stack size method by employing a for loop in C++.

Example

Example

#include <iostream>  

#include <stack>  

using namespace std;    //using standard namespace

int main()   //main function

{  

    std::stack<int> cpptutorial;  

    std::cout << "0. size: "<< cpptutorial.size();  

    for(int j=0; j<5; j++)  

    cpptutorial.push(j);  

    cout<<"\n";  

    std::cout<<"1. size: " << cpptutorial.size();  

    cpptutorial.pop();  

    cout<<"\n";  

    std::cout<<"2. size: "<< cpptutorial.size();  

    return 0;  

}

Output:

Output

0. size: 0

1. size: 5

2. size: 4

Explanation:

In this instance, we've instantiated a stack of integers called cpptutorial to showcase the functionality of the size method in the C++ language. At the start, the stack is devoid of elements, thereby resulting in a size of 0. Subsequently, the push method is employed to add five elements (0 to 4) onto the stack, causing its size to increase to 5. Following this, the pop operation is executed to eliminate a single element, leading to a reduction in the stack's size to 4. Ultimately, this example illustrates the dynamic nature of the stack's size as elements are added to or removed from it.

C++ Stack class example using the size Function

Let's consider an illustration to showcase the functionality of the size method in the stack data structure within C++.

Example

Example

#include <iostream>  

#include <stack>  

using namespace std;    //using standard namespace

int main()   //main function

{  

    stack<int> cpptutorial;  

    cpptutorial.push(28);  

    cpptutorial.push(37);  

    cpptutorial.push(55);  

    cout << cpptutorial.size();  

    return 0;  

}

Output:

Explanation:

In this instance, a stack named cpptutorial has been established to store integers, containing three elements (28, 37, and 55) that were pushed onto it. Subsequently, the size method is employed to determine the current total of elements within the stack. Upon adding the three elements, the result showcases a count of 3.

C++ Stack Example to Compare the Sizes of Two Stacks

Let's consider a basic example to demonstrate the stack size method for comparing the sizes of two stacks in C++.

Example

Example

#include <iostream>  

#include <stack>  

int main()   //main function

{  

    std::stack<int> a,b;  

    a.push(5); a.push(8); a.push(50);  

    b.push(132); b.push(45);  

    std::cout<<"Size of a: "<<a.size();  

    std::cout<<"\nSize of b: " <<b.size();  

    return 0;  

}

Output:

Output

Size of a: 3

Size of b: 2

Explanation:

In this illustration, we've established two stacks of integers, denoted as a and b, with three elements (5, 8, 50) in stack a, and two elements (132, 45) in stack b. Subsequently, the size method is employed to exhibit the quantity of elements in each stack. Consequently, the result indicates that stack a comprises 3 elements, while stack b comprises 2 elements.

Features of the stack::size in C++

The stack size function contains many features in C++. Some of them are as follows:

  • The stack size function in C++ is commonly utilized to return the number of elements that are stored in the stack.
  • If we need to perform operations using the stack size function, it takes only constant time complexity, i.e., O(1).
  • The stack size function in C++ doesn't allow us to modify the content of the stack, but it only provides the details about the stack.
  • We can use this function to work with the stacks that can contain any data type, such as int, float, char, string, and many others.
  • We can utilize the stack size function to check whether the stack is empty or not. It also checks the loop control and iterator based on the number of elements in C++.
  • Conclusion

In summary, the stack::size method in C++ plays a crucial role within the stack container. Its purpose lies in providing insights into and overseeing the current status of the stack container. By indicating the quantity of elements within the stack presently, it enables us to monitor data flow throughout program execution. This function proves invaluable for validating stack actions, regulating loops, and efficiently managing memory in programs reliant on stacks.

One key benefit of utilizing the size method is its constant time complexity of O(1), enabling instant retrieval of the stack's size without the need to iterate over its elements.

C++ Stack size Function FAQs

The stack::size function in C++ returns the number of elements currently stored in the stack.

The stack::size method in C++ is frequently employed to retrieve the current count of elements stored in the stack. This function proves beneficial for observing the stack's status during program execution.

The time complexity of the stack::size function is O(1).

The time complexity associated with the stack size method is O(1) which signifies constant time complexity.

It is safe to invoke the stack::size function on an empty stack in C++.

Yes, it is entirely secure. Invoking the stack::size function on an empty stack will simply yield a result of 0, indicating the absence of any elements within the stack and eliminating the possibility of encountering a runtime error.

No, invoking the stack::size function does not alter the stack in C++.

No, invoking the size method does not alter the initial stack. It is a function for viewing purposes only, providing details on the quantity of elements in the stack.

5) How does the stack::size function differ from the stack::empty function?

The primary distinction between the stack size and the stack empty method lies in their return values. While the size method provides the count of elements in the stack, the empty method yields a Boolean result. It evaluates to true when the stack is devoid of elements and false if there is at least one element present.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience