C++ Queue Library - queue() Function



Description

The C++ copy constructor std::priority_queue::priority_queue() constructs a priority_queue with copy of each elements present in existing priority_queue other.

Declaration

Following is the declaration for std::priority_queue::priority_queue() constructor form std::queue header.

C++11

priority_queue( const priority_queue& other );

Parameters

other − Another priority_queue object of same type.

Return value

Constructor never returns value.

Exceptions

This member function never throws exception.

Time complexity

Linear i.e. O(n)

Example

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

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main(void) {
   vector<int> v = {3, 1, 5, 2, 4};
   priority_queue<int> q1(less<int>(), v);
   priority_queue<int> q2(q1);

   cout << "Contents of q1" << endl;
   while (!q1.empty()) {
      cout << q1.top() << endl;
      q1.pop();
   }

   cout << endl;

   cout << "Contents of q2" << endl;
   while (!q2.empty()) {
      cout << q2.top() << endl;
      q2.pop();
   }

   return 0;
}

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

Contents of q1
5
4
3
2
1
Contents of q2
5
4
3
2
1
queue.htm
Advertisements