C++ Queue Library - size() Function



Description

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

Declaration

Following is the declaration for std::queue::size() function form std::queue header.

C++98

size_type size() const;

Parameters

None

Return value

Returns the total number of elements present in the queue.

Exceptions

No-throw guarantee for standard container types.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::queue::size() function.

#include <iostream>
#include <queue>

using namespace std;

int main(void) {
   queue<int> q;

   for (int i = 0; i < 5; ++i)
      q.push(i + 1);

   cout << "Size of queue = " << q.size() << endl;

   return 0;
}

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

Size of queue = 5
queue.htm
Advertisements