---
name: "std::priority_queue"
section: "3cxx"
platform: "linux"
package: "libstdc++-12-doc"
version: "12.4.0-5"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/std::priority_queue/3cxx"
---

# std::priority\_queue(3cxx)

A standard container automatically sorting its contents.

## NAME

std::priority\_queue\< \_Tp, \_Sequence, \_Compare \> - A standard container automatically sorting its contents.

## SYNOPSIS

`#include <queue>`

### Public Types

**typedef** \_Sequence::const\_reference **const\_reference**\
 **typedef** \_Sequence **container\_type**\
 **typedef** \_Sequence::reference **reference**\
 **typedef** \_Sequence::size\_type **size\_type**\
 **typedef** \_Compare **value\_compare**\
 **typedef** \_Sequence::value\_type **value\_type**\

### Public Member Functions

template\<**typename** **\_Seq**  = \_Sequence, **typename** \_Requires  = typename enable\_if\<\_\_and\_\<is\_default\_constructible\<\_Compare\>,				is\_default\_constructible\<\_Seq\>\>::value\>::type\> **priority\_queue** ()\
 Default constructor creates no elements.  template\<**typename** **\_InputIterator** , **typename** \_Alloc , **typename**  = std::\_RequireInputIter\<\_InputIterator\>, **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (**\_InputIterator** \_\_first, **\_InputIterator** \_\_last, const \_Alloc &**\_\_alloc**)\
 template\<**typename** **\_InputIterator** , **typename**  = std::\_RequireInputIter\<\_InputIterator\>\> **priority\_queue** (**\_InputIterator** \_\_first, **\_InputIterator** \_\_last, const \_Compare &\_\_x, \_Sequence &&**\_\_s**)\
 template\<**typename** **\_InputIterator** , **typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (**\_InputIterator** \_\_first, **\_InputIterator** \_\_last, const \_Compare &\_\_x, \_Sequence &&**\_\_s**, const \_Alloc &**\_\_alloc**)\
 template\<**typename** **\_InputIterator** , **typename** \_Alloc , **typename**  = std::\_RequireInputIter\<\_InputIterator\>, **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (**\_InputIterator** \_\_first, **\_InputIterator** \_\_last, const \_Compare &\_\_x, const \_Alloc &**\_\_alloc**)\
 template\<**typename** **\_InputIterator** , **typename**  = std::\_RequireInputIter\<\_InputIterator\>\> **priority\_queue** (**\_InputIterator** \_\_first, **\_InputIterator** \_\_last, const \_Compare &\_\_x, const \_Sequence &**\_\_s**)\
 template\<**typename** **\_InputIterator** , **typename** \_Alloc , **typename**  = std::\_RequireInputIter\<\_InputIterator\>, **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (**\_InputIterator** \_\_first, **\_InputIterator** \_\_last, const \_Compare &\_\_x, const \_Sequence &**\_\_s**, const \_Alloc &**\_\_alloc**)\
 template\<**typename** **\_InputIterator** , **typename**  = std::\_RequireInputIter\<\_InputIterator\>\> **priority\_queue** (**\_InputIterator** \_\_first, **\_InputIterator** \_\_last, const \_Compare &\_\_x=\_Compare())\
 Builds a queue from a range.  template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (const \_Alloc &\_\_a)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (const \_Compare &\_\_x, \_Sequence &&\_\_c, const \_Alloc &\_\_a)\
 **priority\_queue** (const \_Compare &\_\_x, \_Sequence &&**\_\_s**=\_Sequence())\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (const \_Compare &\_\_x, const \_Alloc &\_\_a)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (const \_Compare &\_\_x, const \_Sequence &\_\_c, const \_Alloc &\_\_a)\
 **priority\_queue** (const \_Compare &\_\_x, const \_Sequence &**\_\_s**)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (const **priority\_queue** &\_\_q, const \_Alloc &\_\_a)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **priority\_queue** (**priority\_queue** &&\_\_q, const \_Alloc &\_\_a)\
 template\<typename... \_Args\> **void** **emplace** (**\_Args** &&... **\_\_args**)\
 bool **empty** () const\
 **void** **pop** ()\
 Removes first element.  **void** **push** (const value\_type &\_\_x)\
 Add data to the queue.  **void** **push** (value\_type &&\_\_x)\
 size\_type **size** () const\
 **void** **swap** (**priority\_queue** &**\_\_pq**) noexcept(\_\_and\_\<//c++1**z** **or** **gnu**++11 **\_\_is\_nothrow\_swappable**\< \_Sequence \>, **\_\_is\_nothrow\_swappable**\< \_Compare \> \>::value)\
 const\_reference **top** () const\

### Protected Attributes

\_Sequence **c**\
 \_Compare **comp**\

## Detailed Description

### template\<typename \_Tp, typename \_Sequence = vector\<\_Tp\>, typename \_Compare = less\<typename \_Sequence::value\_type\>\>

class std::priority\_queue\< \_Tp, \_Sequence, \_Compare \>"A standard container automatically sorting its contents.

**Template Parameters**

*\_Tp* Type of element. \
 *\_Sequence* Type of underlying sequence, defaults to vector\<\_Tp\>. \
 *\_Compare* Comparison function object type, defaults to less\<\_Sequence::value\_type\>.

This is not a true container, but an *adaptor*. It holds another container, and provides a wrapper interface to that container. The wrapper is what enforces priority-based sorting and queue behavior. Very few of the standard container/sequence interface requirements are met (e.g., iterators).

The second template parameter defines the type of the underlying sequence/container. It defaults to std::vector, but it can be any type that supports `front()`, `push_back`, `pop_back`, and random-access iterators, such as std::deque or an appropriate user-defined type.

The third template parameter supplies the means of making priority comparisons. It defaults to `less<value_type>` but can be anything defining a strict weak ordering.

Members not found in *normal* containers are `container_type`, which is a typedef for the second Sequence parameter, and `push`, `pop`, and `top`, which are standard queue operations.

**Note**

No equality/comparison operators are provided for priority\_queue.

Sorting of the elements takes place as they are added to, and removed from, the priority\_queue using the priority\_queue's member functions. If you access the elements by other means, and change their data such that the sorting order would be different, the priority\_queue will not re-sort the elements for you. (How could it know to do so?)

## Constructor & Destructor Documentation

### template\<typename \_Tp , typename \_Sequence = vector\<\_Tp\>, typename \_Compare = less\<typename \_Sequence::value\_type\>\> template\<typename \_Seq = \_Sequence, typename \_Requires = typename enable\_if\<\_\_and\_\<is\_default\_constructible\<\_Compare\>, is\_default\_constructible\<\_Seq\>\>::value\>::type\> std::priority\_queue\< \_Tp, \_Sequence, \_Compare \>::priority\_queue ()` [inline]`

Default constructor creates no elements.

### template\<typename \_Tp , typename \_Sequence = vector\<\_Tp\>, typename \_Compare = less\<typename \_Sequence::value\_type\>\> template\<typename \_InputIterator , typename = std::\_RequireInputIter\<\_InputIterator\>\> std::priority\_queue\< \_Tp, \_Sequence, \_Compare \>::priority\_queue (\_InputIterator \_\_first, \_InputIterator \_\_last, const \_Compare & \_\_x = `_Compare()`)` [inline]`

Builds a queue from a range.

**Parameters**

*\_\_first* An input iterator. \
 *\_\_last* An input iterator. \
 *\_\_x* A comparison functor describing a strict weak ordering. \
 *\_\_s* An initial sequence with which to start.

Begins by copying *\_\_s*, inserting a copy of the elements from \[first,last) into the copy of *\_\_s*, then ordering the copy according to *\_\_x*.

For more information on function objects, see the documentation on **functor base classes**.

## Member Function Documentation

### template\<typename \_Tp , typename \_Sequence = vector\<\_Tp\>, typename \_Compare = less\<typename \_Sequence::value\_type\>\> bool std::priority\_queue\< \_Tp, \_Sequence, \_Compare \>::empty () const` [inline]`

Returns true if the queue is empty.

### template\<typename \_Tp , typename \_Sequence = vector\<\_Tp\>, typename \_Compare = less\<typename \_Sequence::value\_type\>\> void std::priority\_queue\< \_Tp, \_Sequence, \_Compare \>::pop ()` [inline]`

Removes first element. This is a typical queue operation. It shrinks the queue by one. The time complexity of the operation depends on the underlying sequence.

Note that no data is returned, and if the first element's data is needed, it should be retrieved before pop() is called.

### template\<typename \_Tp , typename \_Sequence = vector\<\_Tp\>, typename \_Compare = less\<typename \_Sequence::value\_type\>\> void std::priority\_queue\< \_Tp, \_Sequence, \_Compare \>::push (const value\_type & \_\_x)` [inline]`

Add data to the queue.

**Parameters**

*\_\_x* Data to be added.

This is a typical queue operation. The time complexity of the operation depends on the underlying sequence.

### template\<typename \_Tp , typename \_Sequence = vector\<\_Tp\>, typename \_Compare = less\<typename \_Sequence::value\_type\>\> size\_type std::priority\_queue\< \_Tp, \_Sequence, \_Compare \>::size () const` [inline]`

Returns the number of elements in the queue. \

### template\<typename \_Tp , typename \_Sequence = vector\<\_Tp\>, typename \_Compare = less\<typename \_Sequence::value\_type\>\> const\_reference std::priority\_queue\< \_Tp, \_Sequence, \_Compare \>::top () const` [inline]`

Returns a read-only (constant) reference to the data at the first element of the queue.

## Author

Generated automatically by Doxygen for libstdc++ from the source code.
