std::ranges::make_heap
Aus de.cppreference.net
| Deklariert im Header <algorithm>
|
||
| Aufrufsignatur |
||
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>
constexpr I make_heap( I first, S last, Comp comp = {}, Proj proj = {} );
|
(1) | (seit C++20) |
template< ranges::random_access_range R,
class Comp = ranges::less, class Proj = std::identity >
requires std::sortable<ranges::iterator_t<R>, Comp, Proj>
constexpr ranges::borrowed_iterator_t<R>
make_heap( R&& r, Comp comp = {}, Proj proj = {} );
|
(2) | (seit C++20) |
Konstruiert einen Heap bezüglich comp und proj aus den Elementen im angegebenen Bereich.
1) Der angegebene Bereich ist
[first, last).2) Der angegebene Bereich ist
r.Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithmus-Funktionsobjekte (informell als Niebloids bekannt), das heißt:
- Explizite Vorlagenargumentlisten können beim Aufruf keiner von ihnen angegeben werden.
- Keine von ihnen sind für argumentabhängige Suche sichtbar.
- Wenn eine von ihnen durch normale unqualifizierte Suche als Name links vom Funktionsaufrufoperator gefunden wird, wird argumentabhängige Suche unterdrückt.
Parameter
| first, last | - | das Iterator-Sentinel-Paar, das den Bereich der zu ändernden Elemente definiert |
| r | - |
der
range
der zu ändernden Elemente
|
| comp | - | Komparator, der auf die projizierten Elemente angewendet wird |
| proj | - | Projektion, die auf die Elemente angewendet wird |
Rückgabewert
1)
last
2)
ranges::
end
(
r
)
Komplexität
Höchstens 3·N Anwendungen von comp und 6·N Anwendungen von proj , wobei N ist:
1)
ranges::
distance
(
first, last
)
2)
ranges::
distance
(
r
)
Beispiel
Diesen Code ausführen
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <vector> void out(const auto& what, int n = 1) { while (n-- > 0) std::cout << what; } void print(auto rem, const auto& v) { out(rem); for (auto e : v) out(e), out(' '); out('\n'); } void draw_heap(const auto& v) { auto bails = [](int n, int w) { auto b = [](int w) { out("┌"), out("─", w), out("┴"), out("─", w), out("┐"); }; if (!(n /= 2)) return; for (out(' ', w); n-- > 0;) b(w), out(' ', w + w + 1); out('\n'); }; auto data = [](int n, int w, auto& first, auto last) { for (out(' ', w); n-- > 0 && first != last; ++first) out(*first), out(' ', w + w + 1); out('\n'); }; auto tier = [&](int t, int m, auto& first, auto last) { const int n{1 << t}; const int w{(1 << (m - t - 1)) - 1}; bails(n, w), data(n, w, first, last); }; const int m{static_cast<int>(std::ceil(std::log2(1 + v.size())))}; auto first{v.cbegin()}; for (int i{}; i != m; ++i) tier(i, m, first, v.cend()); } int main() { std::vector h{1, 6, 1, 8, 0, 3, 3, 9, 8, 8, 7, 4, 9, 8, 9}; print("source: ", h); std::ranges::make_heap(h); print("\n" "max-heap: ", h); draw_heap(h); std::ranges::make_heap(h, std::greater{}); print("\n" "min-heap: ", h); draw_heap(h); }
Ausgabe:
source: 1 6 1 8 0 3 3 9 8 8 7 4 9 8 9
max-heap: 9 8 9 8 8 4 9 6 1 0 7 1 3 8 3
9
┌───┴───┐
8 9
┌─┴─┐ ┌─┴─┐
8 8 4 9
┌┴┐ ┌┴┐ ┌┴┐ ┌┴┐
6 1 0 7 1 3 8 3
min-heap: 0 1 1 8 6 3 3 9 8 8 7 4 9 8 9
0
┌───┴───┐
1 1
┌─┴─┐ ┌─┴─┐
8 6 3 3
┌┴┐ ┌┴┐ ┌┴┐ ┌┴┐
9 8 8 7 4 9 8 9
Siehe auch
(C++20) |
prüft, ob der angegebene Bereich ein Max-Heap ist (Algorithmus-Funktionsobjekt) |
(C++20) |
findet den größten Teilbereich, der ein Max-Heap ist (Algorithmus-Funktionsobjekt) |
(C++20) |
fügt einem Max-Heap ein Element hinzu (Algorithmus-Funktionsobjekt) |
(C++20) |
entfernt das größte Element aus einem Max-Heap (Algorithmus-Funktionsobjekt) |
(C++20) |
wandelt einen Max-Heap in einen sortierten Bereich von Elementen um (Algorithmus-Funktionsobjekt) |
| erzeugt einen Max-Heap aus einem Bereich von Elementen (Funktions-Template) |