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

Queue Size Function

BLUF: Mastering Queue 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: Queue Size Function

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

The size function in C++ Queue provides the count of elements stored in the queue. It accurately reflects the current number of elements present, offering a direct insight into the queue's size.

Syntax

Example

size_type size() const;

Parameters

The function doesn't accept any parameters; it solely provides the size of the queue upon execution.

Return value

The function returns the count of elements in the queue, indicating the size of the queue.

Example 1

Example

#include <iostream>
#include <queue>
int main()
{
	std::queue<int> newqueue;
	std::cout<< "0. size: "<< newqueue.size();
	for(int j=0; j<5; j++)
	newqueue.push(j);
	std::cout<<"\n";
	std::cout << "1. size: " << newqueue.size();
	newqueue.pop();
	std::cout<<"\n";
	std::cout << "2. size: "<< newqueue.size();
	return 0;
}

Output:

Output

0.size: 0
1.size: 5
2.size: 4

Example 2

Example

#include <iostream>
#include <queue>
using namespace std;
int main()
{
	int result = 0;
	queue<int> newqueue;
	newqueue.push(12);
	newqueue.push(24);
	newqueue.push(36);
	newqueue.push(48);
	cout<<"Size of the queue is ";
	cout<<newqueue.size();
	return 0;
}

Output:

Output

Size of queue is 4

Complexity

The complexity is constant.

Data races

The function interacts with the container, which in turn allows for the evaluation of the queue's size.

Exception Safety

Assurance is given that it aligns with the actions executed on the base container object.

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