Files
2025-10-25 03:02:53 +03:00

372 lines
20 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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,<vector> and<deque> 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.8Sorting and related operations[alg.sorting]")[.](#priqueue.overview-1.sentence-3)
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: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&&); template<class InputIterator>constexpr priority_queue(InputIterator first, InputIterator last, const Compare& x = Compare()); template<class InputIterator>constexpr priority_queue(InputIterator first, InputIterator last, const Compare& x, const Container&); template<class InputIterator>constexpr priority_queue(InputIterator first, InputIterator last, const Compare& x,
Container&&); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<T> R>constexpr priority_queue(from_range_t, R&& rg, const Compare& x = Compare()); template<class Alloc> constexpr explicit priority_queue(const Alloc&); template<class Alloc> constexpr priority_queue(const Compare&, const Alloc&); template<class Alloc>constexpr priority_queue(const Compare&, const Container&, const Alloc&); template<class Alloc> constexpr priority_queue(const Compare&, Container&&, const Alloc&); template<class Alloc> constexpr priority_queue(const priority_queue&, const Alloc&); template<class Alloc> constexpr priority_queue(priority_queue&&, const Alloc&); template<class InputIterator, class Alloc>constexpr priority_queue(InputIterator, InputIterator, const Alloc&); template<class InputIterator, class Alloc>constexpr priority_queue(InputIterator, InputIterator, const Compare&, const Alloc&); template<class InputIterator, class Alloc>constexpr priority_queue(InputIterator, InputIterator, const Compare&, const Container&, const Alloc&); template<class InputIterator, class Alloc>constexpr priority_queue(InputIterator, InputIterator, const Compare&, Container&&, const Alloc&); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<T> 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.1Introduction[container.intro.reqmts]")<T> 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.1Introduction[container.intro.reqmts]")<T> R>constexpr void push_range(R&& rg); template<class... Args> constexpr void emplace(Args&&... args); constexpr void pop(); constexpr 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](range.refinements#concept:input_range "25.4.6Other range refinements[range.refinements]") 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](range.refinements#concept:input_range "25.4.6Other range refinements[range.refinements]") 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](range.refinements#concept:input_range "25.4.6Other range refinements[range.refinements]") 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 providedtemplate<class T, class Container, class Compare, class Alloc>struct uses_allocator<priority_queue<T, Container, Compare>, Alloc>: uses_allocator<Container, Alloc>::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.8Sorting 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<class InputIterator>
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.8Sorting 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<class InputIterator>
constexpr priority_queue(InputIterator first, InputIterator last, const Compare& x,
const Container& y);
template<class InputIterator>
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.8Sorting 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.1Introduction[container.intro.reqmts]")<T> 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.8Sorting 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<Container>(std::forward<R>(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<container_type, Alloc> is false the constructors in this subclause shall not participate in overload resolution[.](#priqueue.cons.alloc-1.sentence-1)
[🔗](#lib:priority_queue,constructor____)
`template<class Alloc> 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<class Alloc> 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<class Alloc>
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<class Alloc>
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<class Alloc> 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<class Alloc> 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<class InputIterator, class Alloc>
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<class InputIterator, class Alloc>
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<class InputIterator, class Alloc>
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<class InputIterator, class Alloc>
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.1Introduction[container.intro.reqmts]")<T> 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<Container>(std::forward<R>(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.1Introduction[container.intro.reqmts]")<T> 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<Container>(std::forward<R>(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.1Introduction[container.intro.reqmts]")<T> 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<R>(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<class... Args> 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>(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<class T, class Container, class Compare>
constexpr void swap(priority_queue<T, Container, Compare>& x,
priority_queue<T, Container, Compare>& 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<Container> is true andis_swappable_v<Compare> 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)