24 Containers library [containers]

24.6 Container adaptors [container.adaptors]

24.6.7 Class template priority_queue [priority.queue]

24.6.7.1 Overview [priqueue.overview]

Any sequence container with random access iterator and supporting operations front(), push_back() and pop_back() can be used to instantiate priority_queue.
In particular, vector and deque can be used.
Instantiating priority_queue also involves supplying a function or function object for making priority comparisons; the library assumes that the function or function object defines a strict weak ordering.
namespace std { template<class T, class Container = vector<T>, class Compare = less<typename Container::value_type>> class priority_queue { public: using value_type = typename Container::value_type; using reference = typename Container::reference; using const_reference = typename Container::const_reference; using size_type = typename Container::size_type; using container_type = Container; using value_compare = Compare; protected: Container c; Compare comp; public: priority_queue() : priority_queue(Compare()) {} explicit priority_queue(const Compare& x) : priority_queue(x, Container()) {} priority_queue(const Compare& x, const Container&); priority_queue(const Compare& x, Container&&); template<class InputIterator> priority_queue(InputIterator first, InputIterator last, const Compare& x = Compare()); template<class InputIterator> priority_queue(InputIterator first, InputIterator last, const Compare& x, const Container&); template<class InputIterator> priority_queue(InputIterator first, InputIterator last, const Compare& x, Container&&); template<container-compatible-range<T> R> priority_queue(from_range_t, R&& rg, const Compare& x = Compare()); template<class Alloc> explicit priority_queue(const Alloc&); template<class Alloc> priority_queue(const Compare&, const Alloc&); template<class Alloc> priority_queue(const Compare&, const Container&, const Alloc&); template<class Alloc> priority_queue(const Compare&, Container&&, const Alloc&); template<class Alloc> priority_queue(const priority_queue&, const Alloc&); template<class Alloc> priority_queue(priority_queue&&, const Alloc&); template<class InputIterator, class Alloc> priority_queue(InputIterator, InputIterator, const Alloc&); template<class InputIterator, class Alloc> priority_queue(InputIterator, InputIterator, const Compare&, const Alloc&); template<class InputIterator, class Alloc> priority_queue(InputIterator, InputIterator, const Compare&, const Container&, const Alloc&); template<class InputIterator, class Alloc> priority_queue(InputIterator, InputIterator, const Compare&, Container&&, const Alloc&); template<container-compatible-range<T> R, class Alloc> priority_queue(from_range_t, R&& rg, const Compare&, const Alloc&); template<container-compatible-range<T> R, class Alloc> priority_queue(from_range_t, R&& rg, const Alloc&); [[nodiscard]] bool empty() const { return c.empty(); } size_type size() const { return c.size(); } const_reference top() const { return c.front(); } void push(const value_type& x); void push(value_type&& x); template<container-compatible-range<T> R> void push_range(R&& rg); template<class... Args> void emplace(Args&&... args); void pop(); void swap(priority_queue& q) noexcept(is_nothrow_swappable_v<Container> && is_nothrow_swappable_v<Compare>) { using std::swap; swap(c, q.c); swap(comp, q.comp); } }; template<class Compare, class Container> priority_queue(Compare, Container) -> priority_queue<typename Container::value_type, Container, Compare>; template<class InputIterator, class Compare = less<iter-value-type<InputIterator>>, class Container = vector<iter-value-type<InputIterator>>> priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container()) -> priority_queue<iter-value-type<InputIterator>, Container, Compare>; template<ranges::input_range R, class Compare = less<ranges::range_value_t<R>>> priority_queue(from_range_t, R&&, Compare = Compare()) -> priority_queue<ranges::range_value_t<R>, vector<ranges::range_value_t<R>>, Compare>; template<class Compare, class Container, class Allocator> priority_queue(Compare, Container, Allocator) -> priority_queue<typename Container::value_type, Container, Compare>; template<class InputIterator, class Allocator> priority_queue(InputIterator, InputIterator, Allocator) -> priority_queue<iter-value-type<InputIterator>, vector<iter-value-type<InputIterator>, Allocator>, less<iter-value-type<InputIterator>>>; template<class InputIterator, class Compare, class Allocator> priority_queue(InputIterator, InputIterator, Compare, Allocator) -> priority_queue<iter-value-type<InputIterator>, vector<iter-value-type<InputIterator>, Allocator>, Compare>; template<class InputIterator, class Compare, class Container, class Allocator> priority_queue(InputIterator, InputIterator, Compare, Container, Allocator) -> priority_queue<typename Container::value_type, Container, Compare>; template<ranges::input_range R, class Compare, class Allocator> priority_queue(from_range_t, R&&, Compare, Allocator) -> priority_queue<ranges::range_value_t<R>, vector<ranges::range_value_t<R>, Allocator>, Compare>; template<ranges::input_range R, class Allocator> priority_queue(from_range_t, R&&, Allocator) -> priority_queue<ranges::range_value_t<R>, vector<ranges::range_value_t<R>, Allocator>>; // no equality is provided template<class T, class Container, class Compare, class Alloc> struct uses_allocator<priority_queue<T, Container, Compare>, Alloc> : uses_allocator<Container, Alloc>::type { }; }

24.6.7.2 Constructors [priqueue.cons]

priority_queue(const Compare& x, const Container& y); priority_queue(const Compare& x, Container&& y);
Preconditions: x defines a strict weak ordering ([alg.sorting]).
Effects: Initializes comp with x and c with y (copy constructing or move constructing as appropriate); calls make_heap(c.begin(), c.end(), comp).
template<class InputIterator> priority_queue(InputIterator first, InputIterator last, const Compare& x = Compare());
Preconditions: x defines a strict weak ordering ([alg.sorting]).
Effects: Initializes c with first as the first argument and last as the second argument, and initializes comp with x; then calls make_heap(c.begin(), c.end(), comp).
template<class InputIterator> priority_queue(InputIterator first, InputIterator last, const Compare& x, const Container& y); template<class InputIterator> priority_queue(InputIterator first, InputIterator last, const Compare& x, Container&& y);
Preconditions: x defines a strict weak ordering ([alg.sorting]).
Effects: Initializes comp with x and c with y (copy constructing or move constructing as appropriate); calls c.insert(c.end(), first, last); and finally calls make_heap(c.begin(), c.end(), comp).
template<container-compatible-range<T> R> priority_queue(from_range_t, R&& rg, const Compare& x = Compare());
Preconditions: x defines a strict weak ordering ([alg.sorting]).
Effects: Initializes comp with x and c with ranges​::​to<Container>(std​::​forward<R>(rg)) and finally calls make_heap(c.begin(), c.end(), comp).

24.6.7.3 Constructors with allocators [priqueue.cons.alloc]

If uses_allocator_v<container_type, Alloc> is false the constructors in this subclause shall not participate in overload resolution.
template<class Alloc> explicit priority_queue(const Alloc& a);
Effects: Initializes c with a and value-initializes comp.
template<class Alloc> priority_queue(const Compare& compare, const Alloc& a);
Effects: Initializes c with a and initializes comp with compare.
template<class Alloc> priority_queue(const Compare& compare, const Container& cont, const Alloc& a);
Effects: Initializes c with cont as the first argument and a as the second argument, and initializes comp with compare; calls make_heap(c.begin(), c.end(), comp).
template<class Alloc> priority_queue(const Compare& compare, Container&& cont, const Alloc& a);
Effects: Initializes c with std​::​move(cont) as the first argument and a as the second argument, and initializes comp with compare; calls make_heap(c.begin(), c.end(), comp).
template<class Alloc> priority_queue(const priority_queue& q, const Alloc& a);
Effects: Initializes c with q.c as the first argument and a as the second argument, and initializes comp with q.comp.
template<class Alloc> priority_queue(priority_queue&& q, const Alloc& a);
Effects: Initializes c with std​::​move(q.c) as the first argument and a as the second argument, and initializes comp with std​::​move(q.comp).
template<class InputIterator, class Alloc> priority_queue(InputIterator first, InputIterator last, const Alloc& a);
Effects: Initializes c with first as the first argument, last as the second argument, and a as the third argument, and value-initializes comp; calls make_heap(c.begin(), c.end(), comp).
template<class InputIterator, class Alloc> priority_queue(InputIterator first, InputIterator last, const Compare& compare, const Alloc& a);
Effects: Initializes c with first as the first argument, last as the second argument, and a as the third argument, and initializes comp with compare; calls make_heap(c.begin(), c.end(), comp).
template<class InputIterator, class Alloc> priority_queue(InputIterator first, InputIterator last, const Compare& compare, const Container& cont, const Alloc& a);
Effects: Initializes c with cont as the first argument and a as the second argument, and initializes comp with compare; calls c.insert(c.end(), first, last); and finally calls make_heap(c.begin(), c.end(), comp).
template<class InputIterator, class Alloc> priority_queue(InputIterator first, InputIterator last, const Compare& compare, Container&& cont, const Alloc& a);
Effects: Initializes c with std​::​move(cont) as the first argument and a as the second argument, and initializes comp with compare; calls c.insert(c.end(), first, last); and finally calls make_heap(c.begin(), c.end(), comp).
template<container-compatible-range<T> R, class Alloc> priority_queue(from_range_t, R&& rg, const Compare& compare, const Alloc& a);
Effects: Initializes comp with compare and c with ranges​::​to<Container>(std​::​forward<R>(rg), a); calls make_heap(c.begin(), c.end(), comp).
template<container-compatible-range<T> R, class Alloc> priority_queue(from_range_t, R&& rg, const Alloc& a);
Effects: Initializes c with ranges​::​to<Container>(std​::​forward<R>(rg), a); calls make_heap(c.
begin(), c.end(), comp)
.

24.6.7.4 Members [priqueue.members]

void push(const value_type& x);
Effects: As if by: c.push_back(x); push_heap(c.begin(), c.end(), comp);
void push(value_type&& x);
Effects: As if by: c.push_back(std::move(x)); push_heap(c.begin(), c.end(), comp);
template<container-compatible-range<T> R> void push_range(R&& rg);
Effects: Inserts all elements of rg in c via c.append_range(std​::​forward<R>(rg)) if that is a valid expression, or ranges​::​copy(rg, back_inserter(c)) otherwise.
Then restores the heap property as if by make_heap(c.begin(), c.end(), comp).
Postconditions: is_heap(c.begin(), c.end(), comp) is true.
template<class... Args> void emplace(Args&&... args);
Effects: As if by: c.emplace_back(std::forward<Args>(args)...); push_heap(c.begin(), c.end(), comp);
void pop();
Effects: As if by: pop_heap(c.begin(), c.end(), comp); c.pop_back();

24.6.7.5 Specialized algorithms [priqueue.special]

template<class T, class Container, class Compare> void swap(priority_queue<T, Container, Compare>& x, priority_queue<T, Container, Compare>& y) noexcept(noexcept(x.swap(y)));
Constraints: is_swappable_v<Container> is true and is_swappable_v<Compare> is true.
Effects: As if by x.swap(y).