std::priori...iority_queue(3)
π std::priority_queue::priority_queue π
okay bestie, welcome to the C++ Standard Library manual page for the std::priority_queue::priority_queue constructor. Yes, the constructor has its own page, and yes, there are twenty-two overloads in this part. We are so back. π«Ά
β¨ NAME β¨
std::priority_queue::priority_queue β std::priority_queue::priority_queue
πΈ Synopsis πΈ
Constructs a new underlying container of the container adaptor from a variety of data sources. Literally a constructor buffet, no cap.
priority_queue() : priority_queue(Compare(), Container()) {} (1) (since C++11)
explicit priority_queue( const Compare& compare )
: priority_queue(compare, Container()) {} (2) (since C++11)
explicit priority_queue( const Compare& compare = Compare(),
const Container& cont = Container() ); (3) (until C++11)
priority_queue( const Compare& compare, const Container& cont ); (3) (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,
const Compare& compare = Compare() ); (7) (since C++11)
template< class InputIt >
priority_queue( InputIt first, InputIt last,
const Compare& compare = Compare(),
const Container& cont = Container() ); (8) (until C++11)
template< class InputIt >
priority_queue( InputIt first, InputIt last,
const Compare& compare, const Container& cont ); (8) (since C++11)
template< class InputIt >
priority_queue( InputIt first, InputIt last,
const Compare& compare, Container&& cont ); (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,
const Alloc& alloc ); (12) (since C++11)
template< class Alloc >
priority_queue( const Compare& compare, Container&& cont,
const Alloc& alloc ); (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,
const Alloc& alloc ); (17) (since C++11)
template< class InputIt, class Alloc >
priority_queue( InputIt first, InputIt last, const Compare& compare,
const Container& cont, const Alloc& alloc ); (18) (since C++11)
template< class InputIt, class Alloc >
priority_queue( InputIt first, InputIt last, const Compare& compare,
Container&& cont, const Alloc& alloc ); (19) (since C++11)
template< container-compatible-range<T> R >
priority_queue( std::from_range_t, R&& rg,
const Compare& compare = Compare() ); (20) (since C++23)
template< container-compatible-range<T> R, class Alloc >
priority_queue( std::from_range_t, R&& rg,
const Compare& compare, const Alloc& alloc ); (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)
Now the lore, overload by overload:
- Default constructor. Value-initializes the comparator and the underlying container.
- Copy-constructs the comparison functor
compwith the contents ofcompare. Value-initializes the underlying containerc. - Copy-constructs the underlying container
cwith the contents ofcont. Copy-constructs the comparison functorcompwith the contents ofcompare. Callsstd::make_heap(c.begin(), c.end(), comp). This is also the default constructor. (until C++11) - Move-constructs the underlying container
cwithstd::move(cont). Copy-constructs the comparison functorcompwithcompare. Callsstd::make_heap(c.begin(), c.end(), comp). - Copy constructor. The underlying container is copy-constructed with
other.c. The comparison functor is copy-constructed withother.comp. (implicitly declared) - Move constructor. The underlying container is constructed with
std::move(other.c). The comparison functor is constructed withstd::move(other.comp). (implicitly declared)
7β9) Iterator-pair constructors. These overloads participate in overload resolution only if InputIt satisfies LegacyInputIterator.
- Constructs
cas if byc(first, last)andcompfromcompare. Then callsstd::make_heap(c.begin(), c.end(), comp);. - Copy-constructs
cfromcontandcompfromcompare. Then callsc.insert(c.end(), first, last);, and then callsstd::make_heap(c.begin(), c.end(), comp);. - Move-constructs
cfromstd::move(cont)and copy-constructscompfromcompare. Then callsc.insert(c.end(), first, last);, and then callsstd::make_heap(c.begin(), c.end(), comp);.
10β15) Allocator-extended constructors. These overloads participate in overload resolution only if std::uses_allocator<container_type, Alloc>::value is true, that is, if the underlying container is an allocator-aware container (true for all standard library containers).
- Constructs the underlying container using
allocas allocator. Effectively callsc(alloc).compis value-initialized. - Constructs the underlying container using
allocas allocator. Effectively callsc(alloc). Copy-constructscompfromcompare. - Constructs the underlying container with the contents of
contand usingallocas allocator, as if byc(cont, alloc). Copy-constructscompfromcompare. Then callsstd::make_heap(c.begin(), c.end(), comp). - Constructs the underlying container with the contents of
contusing move semantics while usingallocas allocator, as if byc(std::move(cont), alloc). Copy-constructscompfromcompare. Then callsstd::make_heap(c.begin(), c.end(), comp). - Constructs the underlying container with the contents of
other.cand usingallocas allocator. Effectively callsc(other.c, alloc). Copy-constructscompfromother.comp. - Constructs the underlying container with the contents of
otherusing move semantics while utilizingallocas allocator. Effectively callsc(std::move(other.c), alloc). Move-constructscompfromother.comp.
16β19) Allocator-extended iterator-pair constructors. Same as (7β9), except that alloc is used for constructing the underlying container. These overloads participate in overload resolution only if std::uses_allocator<container_type, Alloc>::value is true and InputIt satisfies LegacyInputIterator.
- Initializes
compwithcompareandcwithranges::to<Container>(std::forward<R>(rg)). Then callsstd::make_heap(c.begin(), c.end(), comp). - Initializes
compwithcompareandcwithranges::to<Container>(std::forward<R>(rg), alloc). Then callsstd::make_heap(c.begin(), c.end(), comp). - Initializes
cwithranges::to<Container>(std::forward<R>(rg), alloc). Then callsstd::make_heap(c.begin(), c.end(), comp).
bestie note: how an implementation checks whether a type satisfiesLegacyInputIteratoris unspecified, except that integral types are required to be rejected. so do not try to be cute withintthere. π
π Parameters π
allocβ allocator to use for all memory allocations of the underlying containerotherβ another container adaptor to be used as source to initialize the underlying containercontβ container to be used as source to initialize the underlying containercompareβ the comparison function object to initialize the underlying comparison functorfirst,lastβ a range[first, last)of elements to initialize withrgβ a container compatible range, that is, aninput_rangewhose elements are convertible toT
π¦ Type requirements π¦
Allocmust meet the requirements ofAllocator.Comparemust meet the requirements ofCompare.Containermust meet the requirements ofContainer. The allocator-extended constructors are only defined ifContainermeets the requirements ofAllocatorAwareContainer.InputItmust meet the requirements ofLegacyInputIterator.
β±οΈ Complexity
okay bestie, hereβs the vibe check on the cost of every overload β same numbers, same O(...) energy, no cap:
1,2)β Constant.3,5,12)βO(N)comparisons andO(N)calls to the constructor ofvalue_type, whereNiscont.size().4)βO(N)comparisons, whereNiscont.size().6)β Constant.7,16,17)βO(M)comparisons, whereMisstd::distance(first, last).8,18)βO(N + M)comparisons andO(N)calls to the constructor ofvalue_type, whereNiscont.size()andMisstd::distance(first, last).9)βO(N + M)comparisons, whereNiscont.size()andMisstd::distance(first, last).10,11)β Constant.13)βO(N)comparisons, whereNiscont.size().14)β Linear in size ofother.15)β Constant ifAlloccompares equal to the allocator ofother. Linear in size ofotherotherwise.19)βO(N + M)comparisons and possiblyO(N)calls to the constructor ofvalue_type(present ifAllocdoes not compare equal to the allocator ofother), whereNiscont.size()andMisstd::distance(first, last).20)βO(N)comparisons andO(N)calls to the constructor ofvalue_type, whereNisranges::distance(rg).21,22)β This section is incomplete. The page literally says so π.
π Notes
The feature-test macro table is giving very βtiny but importantβ energy:
- Feature-test macro:
__cpp_lib_containers_ranges- Value:
202202L - Std:
(C++23) - Feature: Ranges-aware construction and insertion; overloads
(20-22)
- Value:
π§ͺ Example
The page says: // Run this code
#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)
π©Ή Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards:
P0935R0β applied toC++11. Behavior as published: default constructor and constructor(4)made implicit. Correct behavior: were explicit.LWG 3506β applied toC++11. Behavior as published: allocator-extended iterator-pair constructors were missing. Correct behavior: added.LWG 3522β applied toC++11. Behavior as published: constraints on iterator-pair constructors were missing. Correct behavior: added.LWG 3529β applied toC++11. Behavior as published: construction from a pair of iterators calledinsert. Correct behavior: constructs the container from them.
π See also
operator=β assigns values to the container adaptor (public member function).
Category: * Todo without reason
okay thatβs the whole page, bestie π«Ά β the footer carries source/version/date: http://cppreference.com, 2024.06.10, and std::priori...iority_queue(3) β¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms