Name

priority_queue class template — Priority queue container adapter

Synopsis

template <typename T, typename Container = vector<T>,
          typename Compare = less<typename Container::value_type> >
class priority_queue {
public:
  typedef typename Container::value_type value_type;
  typedef typename Container::size_type size_type;
  typedef Container container_type;
   
  explicit priority_queue(const Compare& x = Compare(  ),
                          const Container& = Container(  ));
  template <class InputIterator>
  priority_queue(InputIterator first, InputIterator last,
                 const Compare& x = Compare(  ), 
                 const Container& = Container(  ));
  bool empty(  ) const { return c.empty(  ); }
  size_type size(  ) const { return c.size(  ); }
  const value_type& top(  ) const { return c.front(  ); }
  void push(const value_type& x);
  void pop(  );
protected:
  Container c;
  Compare comp;
};

The priority_queue class template is an adapter for any sequence container that supports random access, such as deque and vector. (The default is vector.) The priority queue keeps its elements in heap order, so it requires a comparator (the Compare template parameter).

Because priority_queue is not itself a standard container, it cannot be used with the standard algorithms. (In particular, note the lack of begin and end member functions.) Thus, the priority_queue adapter is useful only for simple needs.

Unlike queue, priority_queue has no comparison operators.

Most of the members of priority_queue are straightforward mappings from a simple queue protocol to the underlying container ...

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.