(C++17) | ||||
Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
std::priority_queue
Member functions | ||||
priority_queue::priority_queue | ||||
Element access | ||||
Capacity | ||||
Modifiers | ||||
(C++23) | ||||
(C++11) | ||||
(C++11) | ||||
Non-member functions | ||||
(C++11) | ||||
Helper classes | ||||
Deduction guides(C++17) |
priority_queue(): priority_queue(Compare(), Container()){} | (1) | (since C++11) |
explicit priority_queue(const Compare& compare) : priority_queue(compare, Container()){} | (2) | (since C++11) |
(3) | ||
explicit priority_queue(const Compare& compare= Compare(), const Container& cont= Container()); | (until C++11) | |
priority_queue(const Compare& compare,const Container& cont); | (since C++11) | |
priority_queue(const Compare& compare, Container&& cont); | (4) | (since C++11) |
priority_queue(const priority_queue& other); | (5) | |
priority_queue( priority_queue&& other); | (6) | (since C++11) |
template<class InputIt> priority_queue( InputIt first, InputIt last, | (7) | (since C++11) |
(8) | ||
template<class InputIt> priority_queue( InputIt first, InputIt last, | (until C++11) | |
template<class InputIt> priority_queue( InputIt first, InputIt last, | (since C++11) | |
template<class InputIt> priority_queue( InputIt first, InputIt last, | (9) | (since C++11) |
template<class Alloc> explicit priority_queue(const Alloc& alloc); | (10) | (since C++11) |
template<class Alloc> priority_queue(const Compare& compare,const Alloc& alloc); | (11) | (since C++11) |
template<class Alloc> priority_queue(const Compare& compare,const Container& cont, | (12) | (since C++11) |
template<class Alloc> priority_queue(const Compare& compare, Container&& cont, | (13) | (since C++11) |
template<class Alloc> priority_queue(const priority_queue& other,const Alloc& alloc); | (14) | (since C++11) |
template<class Alloc> priority_queue( priority_queue&& other,const Alloc& alloc); | (15) | (since C++11) |
template<class InputIt,class Alloc> priority_queue( InputIt first, InputIt last,const Alloc& alloc); | (16) | (since C++11) |
template<class InputIt,class Alloc> priority_queue( InputIt first, InputIt last,const Compare& compare, | (17) | (since C++11) |
template<class InputIt,class Alloc> priority_queue( InputIt first, InputIt last,const Compare& compare, | (18) | (since C++11) |
template<class InputIt,class Alloc> priority_queue( InputIt first, InputIt last,const Compare& compare, | (19) | (since C++11) |
template<container-compatible-range<T> R> priority_queue(std::from_range_t, R&& rg, | (20) | (since C++23) |
template<container-compatible-range<T> R,class Alloc> priority_queue(std::from_range_t, R&& rg, | (21) | (since C++23) |
template<container-compatible-range<T> R,class Alloc> priority_queue(std::from_range_t, R&& rg,const Alloc& alloc); | (22) | (since C++23) |
Constructs new underlying container of the container adaptor from a variety of data sources.
InputIt
satisfiesLegacyInputIterator.Note that how an implementation checks whether a type satisfiesLegacyInputIterator is unspecified, except that integral types are required to be rejected.
Contents |
alloc | - | allocator to use for all memory allocations of the underlying container |
other | - | another container adaptor to be used as source to initialize the underlying container |
cont | - | container to be used as source to initialize the underlying container |
compare | - | the comparison function object to initialize the underlying comparison functor |
first, last | - | the pair of iterators defining therange of elements to initialize with |
rg | - | acontainer compatible range, that is, aninput_range whose elements are convertible toT |
Type requirements | ||
-Alloc must meet the requirements ofAllocator. | ||
-Compare must meet the requirements ofCompare. | ||
-Container must meet the requirements ofContainer. The allocator-extended constructors are only defined ifContainer meets the requirements ofAllocatorAwareContainer. | ||
-InputIt must meet the requirements ofLegacyInputIterator. |
value_type
, where\(\scriptsize N\)N iscont.size().value_type
, where\(\scriptsize N\)N iscont.size() and\(\scriptsize M\)M isstd::distance(first, last).Alloc
compares equal to the allocator ofother. Linear in size ofother otherwise.value_type
(present ifAlloc
does not compare equal to the allocator ofother), where\(\scriptsize N\)N iscont.size() and\(\scriptsize M\)M isstd::distance(first, last).value_type
, where\(\scriptsize N\)N isranges::distance(rg).This section is incomplete |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_containers_ranges | 202202L | (C++23) | Ranges-aware construction and insertion; overloads(20-22) |
#include <complex>#include <functional>#include <iostream>#include <queue>#include <vector> int main(){std::priority_queue<int> pq1; pq1.push(5);std::cout<<"pq1.size() = "<< pq1.size()<<'\n'; std::priority_queue<int> pq2{pq1};std::cout<<"pq2.size() = "<< pq2.size()<<'\n'; std::vector<int> vec{3,1,4,1,5};std::priority_queue<int> pq3{std::less<int>(), vec};std::cout<<"pq3.size() = "<< pq3.size()<<'\n'; for(std::cout<<"pq3 : ";!pq3.empty(); pq3.pop())std::cout<< pq3.top()<<' ';std::cout<<'\n'; // Demo With Custom Comparator: using my_value_t=std::complex<double>;using my_container_t=std::vector<my_value_t>; auto my_comp=[](const my_value_t& z1,const my_value_t& z2){return z2.real()< z1.real();}; std::priority_queue<my_value_t, my_container_t, decltype(my_comp)> pq4{my_comp}; usingnamespace std::complex_literals; pq4.push(5.0+ 1i); pq4.push(3.0+ 2i); pq4.push(7.0+ 3i); for(;!pq4.empty(); pq4.pop()){constauto& z= pq4.top();std::cout<<"pq4.top() = "<< z<<'\n';} // TODO: C++23 range-aware ctors}
Output:
pq1.size() = 1pq2.size() = 1pq3.size() = 5pq3 : 5 4 3 1 1pq4.top() = (3,2)pq4.top() = (5,1)pq4.top() = (7,3)
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P0935R0 | C++11 | default constructor and constructor(4) were explicit | made implicit |
LWG 3506 | C++11 | allocator-extended iterator-pair constructors were missing | added |
LWG 3522 | C++11 | constraints on iterator-pair constructors were missing | added |
LWG 3529 | C++11 | construction from a pair of iterators calledinsert | constructs the container from them |
assigns values to the container adaptor (public member function)[edit] |