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
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
#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:
0.size: 0
1.size: 5
2.size: 4
Example 2
#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:
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.