[priority.queue] # 23 Containers library [[containers]](./#containers) ## 23.6 Container adaptors [[container.adaptors]](container.adaptors#priority.queue) ### 23.6.4 Class template priority_queue [priority.queue] #### [23.6.4.1](#priqueue.overview) Overview [[priqueue.overview]](priqueue.overview) [1](#priqueue.overview-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L15993) Any sequence container with random access iterator and supporting operationsfront(),push_back() andpop_back() can be used to instantiatepriority_queue[.](#priqueue.overview-1.sentence-1) In particular, and can be used[.](#priqueue.overview-1.sentence-2) Instantiatingpriority_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](alg.sorting "26.8 Sorting and related operations [alg.sorting]")[.](#priqueue.overview-1.sentence-3) namespace std {template, class Compare = less>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:constexpr priority_queue() : priority_queue(Compare()) {}constexpr explicit priority_queue(const Compare& x) : priority_queue(x, Container()) {}constexpr priority_queue(const Compare& x, const Container&); constexpr priority_queue(const Compare& x, Container&&); templateconstexpr priority_queue(InputIterator first, InputIterator last, const Compare& x = Compare()); templateconstexpr priority_queue(InputIterator first, InputIterator last, const Compare& x, const Container&); templateconstexpr priority_queue(InputIterator first, InputIterator last, const Compare& x, Container&&); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R>constexpr priority_queue(from_range_t, R&& rg, const Compare& x = Compare()); template constexpr explicit priority_queue(const Alloc&); template constexpr priority_queue(const Compare&, const Alloc&); templateconstexpr priority_queue(const Compare&, const Container&, const Alloc&); template constexpr priority_queue(const Compare&, Container&&, const Alloc&); template constexpr priority_queue(const priority_queue&, const Alloc&); template constexpr priority_queue(priority_queue&&, const Alloc&); templateconstexpr priority_queue(InputIterator, InputIterator, const Alloc&); templateconstexpr priority_queue(InputIterator, InputIterator, const Compare&, const Alloc&); templateconstexpr priority_queue(InputIterator, InputIterator, const Compare&, const Container&, const Alloc&); templateconstexpr priority_queue(InputIterator, InputIterator, const Compare&, Container&&, const Alloc&); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R, class Alloc>constexpr priority_queue(from_range_t, R&& rg, const Compare&, const Alloc&); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R, class Alloc>constexpr priority_queue(from_range_t, R&& rg, const Alloc&); constexpr bool empty() const { return c.empty(); }constexpr size_type size() const { return c.size(); }constexpr const_reference top() const { return c.front(); }constexpr void push(const value_type& x); constexpr void push(value_type&& x); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R>constexpr void push_range(R&& rg); template constexpr void emplace(Args&&... args); constexpr void pop(); constexpr void swap(priority_queue& q)noexcept(is_nothrow_swappable_v && is_nothrow_swappable_v){ using std::swap; swap(c, q.c); swap(comp, q.comp); }}; template priority_queue(Compare, Container)-> priority_queue; template>, class Container = vector<*iter-value-type*>> priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())-> priority_queue<*iter-value-type*, Container, Compare>; template>> priority_queue(from_range_t, R&&, Compare = Compare())-> priority_queue, vector>, Compare>; template priority_queue(Compare, Container, Allocator)-> priority_queue; template priority_queue(InputIterator, InputIterator, Allocator)-> priority_queue<*iter-value-type*, vector<*iter-value-type*, Allocator>, less<*iter-value-type*>>; template priority_queue(InputIterator, InputIterator, Compare, Allocator)-> priority_queue<*iter-value-type*, vector<*iter-value-type*, Allocator>, Compare>; template priority_queue(InputIterator, InputIterator, Compare, Container, Allocator)-> priority_queue; template priority_queue(from_range_t, R&&, Compare, Allocator)-> priority_queue, vector, Allocator>, Compare>; template priority_queue(from_range_t, R&&, Allocator)-> priority_queue, vector, Allocator>>; // no equality is providedtemplatestruct uses_allocator, Alloc>: uses_allocator::type { };} #### [23.6.4.2](#priqueue.cons) Constructors [[priqueue.cons]](priqueue.cons) [🔗](#lib:priority_queue,constructor) `constexpr priority_queue(const Compare& x, const Container& y); constexpr priority_queue(const Compare& x, Container&& y); ` [1](#priqueue.cons-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16141) *Preconditions*: x defines a strict weak ordering ([[alg.sorting]](alg.sorting "26.8 Sorting and related operations"))[.](#priqueue.cons-1.sentence-1) [2](#priqueue.cons-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16145) *Effects*: Initializescomp withx andc withy (copy constructing or move constructing as appropriate); callsmake_heap(c.begin(), c.end(), comp)[.](#priqueue.cons-2.sentence-1) [🔗](#lib:priority_queue,constructor_) `template constexpr priority_queue(InputIterator first, InputIterator last, const Compare& x = Compare()); ` [3](#priqueue.cons-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16163) *Preconditions*: x defines a strict weak ordering ([[alg.sorting]](alg.sorting "26.8 Sorting and related operations"))[.](#priqueue.cons-3.sentence-1) [4](#priqueue.cons-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16167) *Effects*: Initializes c withfirst as the first argument andlast as the second argument, and initializes comp with x; then calls make_heap(c.begin(), c.end(), comp)[.](#priqueue.cons-4.sentence-1) [🔗](#lib:priority_queue,constructor__) `template constexpr priority_queue(InputIterator first, InputIterator last, const Compare& x, const Container& y); template constexpr priority_queue(InputIterator first, InputIterator last, const Compare& x, Container&& y); ` [5](#priqueue.cons-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16187) *Preconditions*: x defines a strict weak ordering ([[alg.sorting]](alg.sorting "26.8 Sorting and related operations"))[.](#priqueue.cons-5.sentence-1) [6](#priqueue.cons-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16191) *Effects*: Initializescomp withx andc withy (copy constructing or move constructing as appropriate); callsc.insert(c.end(), first, last); and finally callsmake_heap(c.begin(), c.end(), comp)[.](#priqueue.cons-6.sentence-1) [🔗](#lib:priority_queue,constructor___) `template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R> constexpr priority_queue(from_range_t, R&& rg, const Compare& x = Compare()); ` [7](#priqueue.cons-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16211) *Preconditions*: x defines a strict weak ordering ([[alg.sorting]](alg.sorting "26.8 Sorting and related operations"))[.](#priqueue.cons-7.sentence-1) [8](#priqueue.cons-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16215) *Effects*: Initializes comp with x andc with ranges​::​to(std​::​forward(rg)) and finally calls make_heap(c.begin(), c.end(), comp)[.](#priqueue.cons-8.sentence-1) #### [23.6.4.3](#priqueue.cons.alloc) Constructors with allocators [[priqueue.cons.alloc]](priqueue.cons.alloc) [1](#priqueue.cons.alloc-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16224) If uses_allocator_v is false the constructors in this subclause shall not participate in overload resolution[.](#priqueue.cons.alloc-1.sentence-1) [🔗](#lib:priority_queue,constructor____) `template constexpr explicit priority_queue(const Alloc& a); ` [2](#priqueue.cons.alloc-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16234) *Effects*: Initializes c with a and value-initializes comp[.](#priqueue.cons.alloc-2.sentence-1) [🔗](#lib:priority_queue,constructor_____) `template constexpr priority_queue(const Compare& compare, const Alloc& a); ` [3](#priqueue.cons.alloc-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16245) *Effects*: Initializes c with a and initializes comp with compare[.](#priqueue.cons.alloc-3.sentence-1) [🔗](#lib:priority_queue,constructor______) `template constexpr priority_queue(const Compare& compare, const Container& cont, const Alloc& a); ` [4](#priqueue.cons.alloc-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16257) *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)[.](#priqueue.cons.alloc-4.sentence-1) [🔗](#lib:priority_queue,constructor_______) `template constexpr priority_queue(const Compare& compare, Container&& cont, const Alloc& a); ` [5](#priqueue.cons.alloc-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16271) *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)[.](#priqueue.cons.alloc-5.sentence-1) [🔗](#lib:priority_queue,constructor________) `template constexpr priority_queue(const priority_queue& q, const Alloc& a); ` [6](#priqueue.cons.alloc-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16284) *Effects*: Initializes c with q.c as the first argument and a as the second argument, and initializes comp with q.comp[.](#priqueue.cons.alloc-6.sentence-1) [🔗](#lib:priority_queue,constructor_________) `template constexpr priority_queue(priority_queue&& q, const Alloc& a); ` [7](#priqueue.cons.alloc-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16296) *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)[.](#priqueue.cons.alloc-7.sentence-1) [🔗](#lib:priority_queue,constructor__________) `template constexpr priority_queue(InputIterator first, InputIterator last, const Alloc& a); ` [8](#priqueue.cons.alloc-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16309) *Effects*: Initializes c withfirst as the first argument,last as the second argument, anda as the third argument, and value-initializes comp; calls make_heap(c.begin(), c.end(), comp)[.](#priqueue.cons.alloc-8.sentence-1) [🔗](#lib:priority_queue,constructor___________) `template constexpr priority_queue(InputIterator first, InputIterator last, const Compare& compare, const Alloc& a); ` [9](#priqueue.cons.alloc-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16327) *Effects*: Initializes c withfirst as the first argument,last as the second argument, anda as the third argument, and initializes comp with compare; calls make_heap(c.begin(), c.end(), comp)[.](#priqueue.cons.alloc-9.sentence-1) [🔗](#lib:priority_queue,constructor____________) `template constexpr priority_queue(InputIterator first, InputIterator last, const Compare& compare, const Container& cont, const Alloc& a); ` [10](#priqueue.cons.alloc-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16345) *Effects*: Initializes c withcont 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)[.](#priqueue.cons.alloc-10.sentence-1) [🔗](#lib:priority_queue,constructor_____________) `template constexpr priority_queue(InputIterator first, InputIterator last, const Compare& compare, Container&& cont, const Alloc& a); ` [11](#priqueue.cons.alloc-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16362) *Effects*: Initializes c withstd​::​move(cont) as the first argument anda 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)[.](#priqueue.cons.alloc-11.sentence-1) [🔗](#lib:priority_queue,constructor______________) `template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R, class Alloc> constexpr priority_queue(from_range_t, R&& rg, const Compare& compare, const Alloc& a); ` [12](#priqueue.cons.alloc-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16379) *Effects*: Initializes comp with compare andc with ranges​::​to(std​::​forward(rg), a); calls make_heap(c.begin(), c.end(), comp)[.](#priqueue.cons.alloc-12.sentence-1) [🔗](#lib:priority_queue,constructor_______________) `template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R, class Alloc> constexpr priority_queue(from_range_t, R&& rg, const Alloc& a); ` [13](#priqueue.cons.alloc-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16393) *Effects*: Initializesc with ranges​::​to(std​::​forward(rg), a) and value-initializes comp; calls make_heap(c.begin(), c.end(), comp)[.](#priqueue.cons.alloc-13.sentence-1) #### [23.6.4.4](#priqueue.members) Members [[priqueue.members]](priqueue.members) [🔗](#lib:push,priority_queue) `constexpr void push(const value_type& x); ` [1](#priqueue.members-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16409) *Effects*: As if by:c.push_back(x); push_heap(c.begin(), c.end(), comp); [🔗](#lib:push,priority_queue_) `constexpr void push(value_type&& x); ` [2](#priqueue.members-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16424) *Effects*: As if by:c.push_back(std::move(x)); push_heap(c.begin(), c.end(), comp); [🔗](#lib:push_range,priority_queue) `template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R> constexpr void push_range(R&& rg); ` [3](#priqueue.members-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16440) *Effects*: Inserts all elements of rg in c viac.append_range(std​::​forward(rg)) if that is a valid expression, orranges​::​copy(rg, back_inserter(c)) otherwise[.](#priqueue.members-3.sentence-1) Then restores the heap property as if bymake_heap(c.begin(), c.end(), comp)[.](#priqueue.members-3.sentence-2) [4](#priqueue.members-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16448) *Postconditions*: is_heap(c.begin(), c.end(), comp) is true[.](#priqueue.members-4.sentence-1) [🔗](#lib:emplace,priority_queue) `template constexpr void emplace(Args&&... args); ` [5](#priqueue.members-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16459) *Effects*: As if by:c.emplace_back(std::forward(args)...); push_heap(c.begin(), c.end(), comp); [🔗](#lib:pop,priority_queue) `constexpr void pop(); ` [6](#priqueue.members-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16474) *Effects*: As if by:pop_heap(c.begin(), c.end(), comp); c.pop_back(); #### [23.6.4.5](#priqueue.special) Specialized algorithms [[priqueue.special]](priqueue.special) [🔗](#lib:swap,priority_queue) `template constexpr void swap(priority_queue& x, priority_queue& y) noexcept(noexcept(x.swap(y))); ` [1](#priqueue.special-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16493) *Constraints*: is_swappable_v is true andis_swappable_v is true[.](#priqueue.special-1.sentence-1) [2](#priqueue.special-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16498) *Effects*: As if by x.swap(y)[.](#priqueue.special-2.sentence-1)