C++ Queue Library - size() Function



Description

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

Declaration

Following is the declaration for std::priority_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 priority_queue.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

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

#include <iostream>
#include <queue>

using namespace std;

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

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

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

   cout << "After push opration size of queue = " << q.size() << endl;

   return 0;
}

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

Initial size of queue = 0
After push opration size of queue = 5
queue.htm
Advertisements