std::ranges::pop_heap(3)
std::ranges::pop_heap(3) is the title, and it comes from the C++ Standard Libary manual β yes, βLibaryβ is exactly how the header spells it, bestie, and we respect the lore. π
π NAME π
std::ranges::pop_heap β std::ranges::pop_heap. Thatβs the name. Thatβs the whole name. Iconic behavior, honestly. β¨
πΈ Synopsis πΈ
Defined in header <algorithm>. Call signature goes a little like this, and yes, weβre keeping every template detail because she is main character energy:
Defined in header <algorithm>
Call signature
template< std::random_access_iterator I, std::sentinel_for<I> S,
class Comp = ranges::less, class Proj = std::identity >
requires std::sortable<I, Comp, Proj>
(1) (since C++20)
constexpr I
pop_heap( I first, S last, Comp comp = {}, Proj proj = {} );
template< ranges::random_access_range R, class Comp =
ranges::less,
class Proj = std::identity >
(2) (since C++20)
requires std::sortable<ranges::iterator_t<R>, Comp, Proj>
constexpr ranges::borrowed_iterator_t<R>
pop_heap( R&& r, Comp comp = {}, Proj proj = {} );
Okay so hereβs the vibe: it swaps the value in the position first and the value in the position last - 1 and makes the subrange [first, last - 1) into a max heap. This has the effect of removing the first element from the heap defined by the range [first, last). Slay. π
- Elements are compared using the given binary comparison function
compand projection objectproj. - Same as (1), but uses
ras the range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions. No cap. π«Ά
π Parameters π
first,lastβ the range of elements defining the valid nonempty heap to modify.rβ the range of elements defining the valid nonempty heap to modify.predβ predicate to apply to the projected elements.projβ projection to apply to the elements.
β Return value β
An iterator equal to last. Thatβs it, bestie β she returns last and minds her business. π
π₯ Complexity π₯
Given N = ranges::distance(first, last), at most 2log(N) comparisons and 4log(N) projections.
The page literally spells the math as \(\scriptsize 2\log{(N)}\)2log(N) for the comparisons and \(\scriptsize 4\log{(N)}\)4log(N) for the projections, so there you go β every little notation crumb is here. π
π¦ Notes π¦
A max heap is a range of elements [f, l), arranged with respect to comparator comp and projection proj, that has the following properties:
- With
N = l - f,p = f[(i - 1) / 2], andq = f[i], for all0 < i < N, the expressionstd::invoke(comp, std::invoke(proj, p), std::invoke(proj, q))evaluates tofalse. - A new element can be added using
ranges::push_heap, in\(\scriptsize \mathcal{O}(\log N)\)π(log N)time. - The first element can be removed using
ranges::pop_heap, in\(\scriptsize \mathcal{O}(\log N)\)π(log N)time.
π Example π
// Run this code
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string_view>
template<class I = int*>
void print(std::string_view rem, I first = {}, I last = {},
std::string_view term = "\n")
{
for (std::cout << rem; first != last; ++first)
std::cout << *first << ' ';
std::cout << term;
}
int main()
{
std::array v {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
print("initially, v: ", v.cbegin(), v.cend());
std::ranges::make_heap(v);
print("make_heap, v: ", v.cbegin(), v.cend());
print("convert heap into sorted array:");
for (auto n {std::ssize(v)}; n >= 0; --n)
{
std::ranges::pop_heap(v.begin(), v.begin() + n);
print("[ ", v.cbegin(), v.cbegin() + n, "] ");
print("[ ", v.cbegin() + n, v.cend(), "]\n");
}
}
π Output π
initially, v: 3 1 4 1 5 9 2 6 5 3
make_heap, v: 9 6 4 5 5 3 2 1 1 3
convert heap into sorted array:
[ 6 5 4 3 5 3 2 1 1 9 ] [ ]
[ 5 5 4 3 1 3 2 1 6 ] [ 9 ]
[ 5 3 4 1 1 3 2 5 ] [ 6 9 ]
[ 4 3 3 1 1 2 5 ] [ 5 6 9 ]
[ 3 2 3 1 1 4 ] [ 5 5 6 9 ]
[ 3 2 1 1 3 ] [ 4 5 5 6 9 ]
[ 2 1 1 3 ] [ 3 4 5 5 6 9 ]
[ 1 1 2 ] [ 3 3 4 5 5 6 9 ]
[ 1 1 ] [ 2 3 3 4 5 5 6 9 ]
[ 1 ] [ 1 2 3 3 4 5 5 6 9 ]
[ ] [ 1 1 2 3 3 4 5 5 6 9 ]
π See also π
ranges::push_heapβ adds an element to a max heap (C++20) (niebloid)ranges::is_heapβ checks if the given range is a max heap (C++20) (niebloid)ranges::is_heap_untilβ finds the largest subrange that is a max heap (C++20) (niebloid)ranges::make_heapβ creates a max heap out of a range of elements (C++20) (niebloid)ranges::sort_heapβ turns a max heap into a range of elements sorted in ascending order (C++20) (niebloid)pop_heapβ removes the largest element from a max heap (function template)
This lil page is from http://cppreference.com, dated 2024.06.10 β mwah, kisses to the footer, periodt. πβ¨
π¦ 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