Files
cppdraft_translate/cppdraft/priority/queue.md
2025-10-25 03:02:53 +03:00

20 KiB
Raw Blame History

[priority.queue]

23 Containers library [containers]

23.6 Container adaptors [container.adaptors]

23.6.4 Class template priority_queue [priority.queue]

23.6.4.1 Overview [priqueue.overview]

1

#

Any sequence container with random access iterator and supporting operationsfront(),push_back() andpop_back() can be used to instantiatepriority_queue.

In particular, and can be used.

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.

namespace std {template<class T, class Container = vector, 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 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&); 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 R, class Alloc>constexpr priority_queue(from_range_t, R&& rg, const Compare&, const Alloc&); template<container-compatible-range 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 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 && is_nothrow_swappable_v){ 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>, class Container = vector<iter-value-type>> priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())-> priority_queue<iter-value-type, Container, Compare>; template<ranges::input_range R, class Compare = less<ranges::range_value_t>> priority_queue(from_range_t, R&&, Compare = Compare())-> priority_queue<ranges::range_value_t, vector<ranges::range_value_t>, 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, vector<iter-value-type, Allocator>, less<iter-value-type>>; template<class InputIterator, class Compare, class Allocator> priority_queue(InputIterator, InputIterator, Compare, Allocator)-> priority_queue<iter-value-type, vector<iter-value-type, 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, vector<ranges::range_value_t, Allocator>, Compare>; template<ranges::input_range R, class Allocator> priority_queue(from_range_t, R&&, Allocator)-> priority_queue<ranges::range_value_t, vector<ranges::range_value_t, 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 Constructors [priqueue.cons]

🔗

constexpr priority_queue(const Compare& x, const Container& y); constexpr priority_queue(const Compare& x, Container&& y);

1

#

Preconditions: x defines a strict weak ordering ([alg.sorting]).

2

#

Effects: Initializescomp withx andc withy (copy constructing or move constructing as appropriate); callsmake_heap(c.begin(), c.end(), comp).

🔗

template<class InputIterator> constexpr priority_queue(InputIterator first, InputIterator last, const Compare& x = Compare());

3

#

Preconditions: x defines a strict weak ordering ([alg.sorting]).

4

#

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).

🔗

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

#

Preconditions: x defines a strict weak ordering ([alg.sorting]).

6

#

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).

🔗

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

#

Preconditions: x defines a strict weak ordering ([alg.sorting]).

8

#

Effects: Initializes comp with x andc with ranges::to(std::forward(rg)) and finally calls make_heap(c.begin(), c.end(), comp).

23.6.4.3 Constructors with allocators [priqueue.cons.alloc]

1

#

If uses_allocator_v<container_type, Alloc> is false the constructors in this subclause shall not participate in overload resolution.

🔗

template<class Alloc> constexpr explicit priority_queue(const Alloc& a);

2

#

Effects: Initializes c with a and value-initializes comp.

🔗

template<class Alloc> constexpr priority_queue(const Compare& compare, const Alloc& a);

3

#

Effects: Initializes c with a and initializes comp with compare.

🔗

template<class Alloc> constexpr priority_queue(const Compare& compare, const Container& cont, const Alloc& a);

4

#

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> constexpr priority_queue(const Compare& compare, Container&& cont, const Alloc& a);

5

#

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> constexpr priority_queue(const priority_queue& q, const Alloc& a);

6

#

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> constexpr priority_queue(priority_queue&& q, const Alloc& a);

7

#

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> constexpr priority_queue(InputIterator first, InputIterator last, const Alloc& a);

8

#

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).

🔗

template<class InputIterator, class Alloc> constexpr priority_queue(InputIterator first, InputIterator last, const Compare& compare, const Alloc& a);

9

#

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).

🔗

template<class InputIterator, class Alloc> constexpr priority_queue(InputIterator first, InputIterator last, const Compare& compare, const Container& cont, const Alloc& a);

10

#

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).

🔗

template<class InputIterator, class Alloc> constexpr priority_queue(InputIterator first, InputIterator last, const Compare& compare, Container&& cont, const Alloc& a);

11

#

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).

🔗

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

#

Effects: Initializes comp with compare andc with ranges::to(std::forward(rg), a); calls make_heap(c.begin(), c.end(), comp).

🔗

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

#

Effects: Initializesc with ranges::to(std::forward(rg), a) and value-initializes comp; calls make_heap(c.begin(), c.end(), comp).

23.6.4.4 Members [priqueue.members]

🔗

constexpr void push(const value_type& x);

1

#

Effects: As if by:c.push_back(x); push_heap(c.begin(), c.end(), comp);

🔗

constexpr void push(value_type&& x);

2

#

Effects: As if by:c.push_back(std::move(x)); push_heap(c.begin(), c.end(), comp);

🔗

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

#

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.

Then restores the heap property as if bymake_heap(c.begin(), c.end(), comp).

4

#

Postconditions: is_heap(c.begin(), c.end(), comp) is true.

🔗

template<class... Args> constexpr void emplace(Args&&... args);

5

#

Effects: As if by:c.emplace_back(std::forward(args)...); push_heap(c.begin(), c.end(), comp);

🔗

constexpr void pop();

6

#

Effects: As if by:pop_heap(c.begin(), c.end(), comp); c.pop_back();

23.6.4.5 Specialized algorithms [priqueue.special]

🔗

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

#

Constraints: is_swappable_v is true andis_swappable_v is true.

2

#

Effects: As if by x.swap(y).