Difference between revisions of "cpp/container/priority queue/priority queue"
Andreas Krug (Talk | contribs) m (fmt, @-@ -> @,@) |
|||
(One intermediate revision by one user not shown) | |||
Line 3: | Line 3: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl|num=1|since=c++11| | {{dcl|num=1|since=c++11| | ||
− | priority_queue() : priority_queue(Compare(), Container()) { } | + | priority_queue() : priority_queue(Compare(), Container()) {} |
}} | }} | ||
{{dcl|num=2|since=c++11| | {{dcl|num=2|since=c++11| | ||
explicit priority_queue( const Compare& compare ) | explicit priority_queue( const Compare& compare ) | ||
− | : priority_queue(compare, Container()) { } | + | : priority_queue(compare, Container()) {} |
}} | }} | ||
{{dcl rev multi|num=3 | {{dcl rev multi|num=3 | ||
Line 119: | Line 119: | ||
@5@ [[cpp/language/copy constructor|Copy constructor]]. The underlying container is copy-constructed with {{c|other.c}}. The comparison functor is copy-constructed with {{c|other.comp}}. {{mark implicit}} | @5@ [[cpp/language/copy constructor|Copy constructor]]. The underlying container is copy-constructed with {{c|other.c}}. The comparison functor is copy-constructed with {{c|other.comp}}. {{mark implicit}} | ||
− | @6@ [[cpp/language/move constructor|Move constructor]]. The underlying container is constructed with {{c|std::move(other.c)}}.The comparison functor is constructed with {{c|std::move(other.comp)}}. {{mark implicit}} | + | @6@ [[cpp/language/move constructor|Move constructor]]. The underlying container is constructed with {{c|std::move(other.c)}}. The comparison functor is constructed with {{c|std::move(other.comp)}}. {{mark implicit}} |
@7-9@ Iterator-pair constructors. {{cpp/enable_if|{{tt|InputIt}} satisfies {{named req|InputIterator}}|plural=yes}}. | @7-9@ Iterator-pair constructors. {{cpp/enable_if|{{tt|InputIt}} satisfies {{named req|InputIterator}}|plural=yes}}. | ||
Line 139: | Line 139: | ||
@13@ Constructs the underlying container with the contents of {{c|cont}} using move semantics while using {{c|alloc}} as allocator, as if by {{c|c(std::move(cont), alloc)}}. Copy-constructs {{c|comp}} from {{c|compare}}. Then calls {{c|std::make_heap(c.begin(), c.end(), comp)}}. | @13@ Constructs the underlying container with the contents of {{c|cont}} using move semantics while using {{c|alloc}} as allocator, as if by {{c|c(std::move(cont), alloc)}}. Copy-constructs {{c|comp}} from {{c|compare}}. Then calls {{c|std::make_heap(c.begin(), c.end(), comp)}}. | ||
− | @14@Constructs the underlying container with the contents of {{c|other.c}} and using {{c|alloc}} as allocator. Effectively calls {{c|c(other.c, alloc)}}. Copy-constructs {{c|comp}} from {{c|other.comp}}. | + | @14@ Constructs the underlying container with the contents of {{c|other.c}} and using {{c|alloc}} as allocator. Effectively calls {{c|c(other.c, alloc)}}. Copy-constructs {{c|comp}} from {{c|other.comp}}. |
@15@ Constructs the underlying container with the contents of {{c|other}} using move semantics while utilizing {{c|alloc}} as allocator. Effectively calls {{c|c(std::move(other.c), alloc)}}. Move-constructs {{c|comp}} from {{c|other.comp}}. | @15@ Constructs the underlying container with the contents of {{c|other}} using move semantics while utilizing {{c|alloc}} as allocator. Effectively calls {{c|c(std::move(other.c), alloc)}}. Move-constructs {{c|comp}} from {{c|other.comp}}. | ||
Line 158: | Line 158: | ||
{{par|compare|the comparison function object to initialize the underlying comparison functor}} | {{par|compare|the comparison function object to initialize the underlying comparison functor}} | ||
{{par|first, last|a range {{range|first|last}} of elements to initialize with}} | {{par|first, last|a range {{range|first|last}} of elements to initialize with}} | ||
− | {{par|rg|a {{ls|cpp/ranges/to | + | {{par|rg|a {{ls|cpp/ranges/to#container compatible range}}, that is, an {{lconcept|input_range}} whose elements are convertible to {{tt|T}}}} |
{{par hreq}} | {{par hreq}} | ||
{{par req named|Alloc|Allocator}} | {{par req named|Alloc|Allocator}} | ||
Line 177: | Line 177: | ||
@7,16,17@ {{mathjax-or|\(\scriptsize \mathcal{O}{(M)}\)|O(M)}} comparisons, where {{mathjax-or|\(\scriptsize M\)|M}} is {{c|std::distance(first, last)}}. | @7,16,17@ {{mathjax-or|\(\scriptsize \mathcal{O}{(M)}\)|O(M)}} comparisons, where {{mathjax-or|\(\scriptsize M\)|M}} is {{c|std::distance(first, last)}}. | ||
− | @8,18@ {{mathjax-or|\(\scriptsize \mathcal{O}{(N+M)}\)|O(N+M)}} comparisons and {{mathjax-or|\(\scriptsize \mathcal{O}{(N)}\)|O(N)}} calls to the constructor of {{tt|value_type}}, where {{mathjax-or|\(\scriptsize N\)|N}} is {{c|cont.size()}} and {{mathjax-or|\(\scriptsize M\)|M}} is {{c|std::distance(first, last)}}. | + | @8,18@ {{mathjax-or|\(\scriptsize \mathcal{O}{(N + M)}\)|O(N + M)}} comparisons and {{mathjax-or|\(\scriptsize \mathcal{O}{(N)}\)|O(N)}} calls to the constructor of {{tt|value_type}}, where {{mathjax-or|\(\scriptsize N\)|N}} is {{c|cont.size()}} and {{mathjax-or|\(\scriptsize M\)|M}} is {{c|std::distance(first, last)}}. |
− | @9@ {{mathjax-or|\(\scriptsize \mathcal{O}{(N+M)}\)|O(N+M)}} comparisons, where {{mathjax-or|\(\scriptsize N\)|N}} is {{c|cont.size()}} and {{mathjax-or|\(\scriptsize M\)|M}} is {{c|std::distance(first, last)}}. | + | @9@ {{mathjax-or|\(\scriptsize \mathcal{O}{(N + M)}\)|O(N + M)}} comparisons, where {{mathjax-or|\(\scriptsize N\)|N}} is {{c|cont.size()}} and {{mathjax-or|\(\scriptsize M\)|M}} is {{c|std::distance(first, last)}}. |
@10,11@ Constant. | @10,11@ Constant. | ||
Line 189: | Line 189: | ||
@15@ Constant if {{tt|Alloc}} compares equal to the allocator of {{c|other}}. Linear in size of {{c|other}} otherwise. | @15@ Constant if {{tt|Alloc}} compares equal to the allocator of {{c|other}}. Linear in size of {{c|other}} otherwise. | ||
− | @19@ {{mathjax-or|\(\scriptsize \mathcal{O}{(N+M)}\)|O(N+M)}} comparisons and possibly {{mathjax-or|\(\scriptsize \mathcal{O}{(N)}\)|O(N)}} calls to the constructor of {{tt|value_type}} (present if {{tt|Alloc}} does not compare equal to the allocator of {{c|other}}), where {{mathjax-or|\(\scriptsize N\)|N}} is {{c|cont.size()}} and {{mathjax-or|\(\scriptsize M\)|M}} is {{c|std::distance(first, last)}}. | + | @19@ {{mathjax-or|\(\scriptsize \mathcal{O}{(N + M)}\)|O(N + M)}} comparisons and possibly {{mathjax-or|\(\scriptsize \mathcal{O}{(N)}\)|O(N)}} calls to the constructor of {{tt|value_type}} (present if {{tt|Alloc}} does not compare equal to the allocator of {{c|other}}), where {{mathjax-or|\(\scriptsize N\)|N}} is {{c|cont.size()}} and {{mathjax-or|\(\scriptsize M\)|M}} is {{c|std::distance(first, last)}}. |
@20@ {{mathjax-or|\(\scriptsize \mathcal{O}{(N)}\)|O(N)}} comparisons and {{mathjax-or|\(\scriptsize \mathcal{O}{(N)}\)|O(N)}} calls to the constructor of {{tt|value_type}}, where {{mathjax-or|\(\scriptsize N\)|N}} is {{c|ranges::distance(rg)}}. | @20@ {{mathjax-or|\(\scriptsize \mathcal{O}{(N)}\)|O(N)}} comparisons and {{mathjax-or|\(\scriptsize \mathcal{O}{(N)}\)|O(N)}} calls to the constructor of {{tt|value_type}}, where {{mathjax-or|\(\scriptsize N\)|N}} is {{c|ranges::distance(rg)}}. | ||
− | @21 | + | @21,22@ {{todo}} |
===Notes=== | ===Notes=== | ||
Line 223: | Line 223: | ||
std::cout << pq3.top() << ' '; | std::cout << pq3.top() << ' '; | ||
std::cout << '\n'; | std::cout << '\n'; | ||
− | |||
// Demo With Custom Comparator: | // Demo With Custom Comparator: | ||
Line 237: | Line 236: | ||
std::priority_queue<my_value_t, | std::priority_queue<my_value_t, | ||
my_container_t, | my_container_t, | ||
− | decltype(my_comp)> pq4 {my_comp}; | + | decltype(my_comp)> pq4{my_comp}; |
using namespace std::complex_literals; | using namespace std::complex_literals; |
Latest revision as of 01:28, 4 November 2023
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
satisfies LegacyInputIterator.Note that how an implementation checks whether a type satisfies LegacyInputIterator is unspecified, except that integral types are required to be rejected.
Contents |
[edit] Parameters
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 | - | a range [ first, last) of elements to initialize with
|
rg | - | a container compatible range, that is, an input_range whose elements are convertible to T
|
Type requirements | ||
-Alloc must meet the requirements of Allocator.
| ||
-Compare must meet the requirements of Compare.
| ||
-Container must meet the requirements of Container. The allocator-extended constructors are only defined if Container meets the requirements of AllocatorAwareContainer.
| ||
-InputIt must meet the requirements of LegacyInputIterator.
|
[edit] Complexity
value_type
, where N is cont.size().value_type
, where N is cont.size() and M is std::distance(first, last).Alloc
compares equal to the allocator of other. Linear in size of other otherwise.value_type
(present if Alloc
does not compare equal to the allocator of other), where N is cont.size() and M is std::distance(first, last).value_type
, where N is ranges::distance(rg).This section is incomplete |
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L | (C++23) | Ranges-aware construction and insertion; overloads (20-22) |
[edit] Example
#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}; using namespace std::complex_literals; pq4.push(5.0 + 1i); pq4.push(3.0 + 2i); pq4.push(7.0 + 3i); for (; !pq4.empty(); pq4.pop()) { const auto& z = pq4.top(); std::cout << "pq4.top() = " << z << '\n'; } // TODO: C++23 range-aware ctors }
Output:
pq1.size() = 1 pq2.size() = 1 pq3.size() = 5 pq3 : 5 4 3 1 1 pq4.top() = (3,2) pq4.top() = (5,1) pq4.top() = (7,3)
[edit] Defect reports
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 called insert
|
constructs the container from them |
[edit] See also
assigns values to the container adaptor (public member function) |