Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
priority_queue
priority_queue::priority_queue
member functions:
priority_queue::empty
priority_queue::pop
priority_queue::push
priority_queue::size
priority_queue::top


priority_queue::priority_queue

public member function
explicit priority_queue ( const Compare& x = Compare(),
                          const Container& y = Container() );
template <class InputIterator>
         priority_queue ( InputIterator first, InputIterator last,
                          const Compare& x = Compare(),
                          const Container& y = Container() );

Construct priority queue

Constructs a priority_queue container adaptor object.

A container adaptor keeps a container object as data. This container object is a copy of parameter y, if any, otherwise it is an empty container.

Because priority_queues satisfy the heap property the element popped from it is always the highest in the container. But the exact ordering criterium to determine this may be any that follow a strict weak ordering, which may be modified with an appropiate y parameter.

The function effectively initializes the comparison and container objects and calls algorithm make_heap accordingly.

Parameters

x
Comparison object to be used for the strict weak ordering.
Compare is the third class template parameter (see class description).
y
Container object.
Container is the second class template parameter (the type of the underlying container for the priority_queue; by default: vector<T>, see class description).
first,last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
The function template type can be any type of input iterator.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// constructing priority queues
#include <iostream>
#include <queue>
using namespace std;
class mycomparison
{
  bool reverse;
public:
  mycomparison(const bool& revparam=false)
    {reverse=revparam;}
  bool operator() (const int& lhs, const int&rhs) const
  {
    if (reverse) return (lhs>rhs);
    else return (lhs<rhs);
  }
};
int main ()
{
  int myints[]= {10,60,50,20};
  priority_queue<int> first;
  priority_queue<int> second (myints,myints+3);
  priority_queue< int, vector<int>, greater<int> > third (myints,myints+3);
  // using mycomparison:
  priority_queue< int, vector<int>, mycomparison > fourth;
  typedef priority_queue<int,vector<int>,mycomparison> mypq_type;
  mypq_type fifth (mycomparison());
  mypq_type sixth (mycomparison(true));
  return 0;
}

The example does not produce any output, but it constructs different priority_queue objects:
First is empty.
Second contains the four ints defined for myints, with 60 (the highest) at its top.
Third has the same four ints, but because it uses greater instead of the default (which is less), it has 10 as its top element.
Fourth, fifth and sixth are very similar to first: they are all empty, except that these use mycomparison for comparisons, which is a special comparison function that behaves differently depending on a flag set on construction.

Complexity

Up to linear in the number of initial elements.