Namespaces
Variants

std::priority_queue<T,Container,Compare>:: priority_queue

From cppreference.net
(Anmerkung: Der bereitgestellte HTML-Code enthält keinen übersetzbaren Text, da alle Tags leer sind. Die Struktur bleibt unverändert, wie angefordert.)
priority_queue ( ) : priority_queue ( Compare ( ) , Container ( ) ) { }
(1) (seit C++11)
explicit priority_queue ( const Compare & compare )
: priority_queue ( compare, Container ( ) ) { }
(2) (seit C++11)
(3)
explicit priority_queue ( const Compare & compare = Compare ( ) ,
const Container & cont = Container ( ) ) ;
(bis C++11)
priority_queue ( const Compare & compare, const Container & cont ) ;
(seit C++11)
priority_queue ( const Compare & compare, Container && cont ) ;
(4) (seit C++11)
priority_queue ( const priority_queue & other ) ;
(5)
priority_queue ( priority_queue && other ) ;
(6) (seit C++11)
template < class InputIt >

priority_queue ( InputIt first, InputIt last,

const Compare & compare = Compare ( ) ) ;
(7) (seit C++11)
(8)
template < class InputIt >

priority_queue ( InputIt first, InputIt last,
const Compare & compare = Compare ( ) ,

const Container & cont = Container ( ) ) ;
(bis C++11)
template < class InputIt >

priority_queue ( InputIt first, InputIt last,

const Compare & compare, const Container & cont ) ;
(seit C++11)
template < class InputIt >

priority_queue ( InputIt first, InputIt last,

const Compare & compare, Container && cont ) ;
(9) (seit C++11)
template < class Alloc >
explicit priority_queue ( const Alloc & alloc ) ;
(10) (seit C++11)
template < class Alloc >
priority_queue ( const Compare & compare, const Alloc & alloc ) ;
(11) (seit C++11)
template < class Alloc >

priority_queue ( const Compare & compare, const Container & cont,

const Alloc & alloc ) ;
(12) (seit C++11)
template < class Alloc >

priority_queue ( const Compare & compare, Container && cont,

const Alloc & alloc ) ;
(13) (seit C++11)
template < class Alloc >
priority_queue ( const priority_queue & other, const Alloc & alloc ) ;
(14) (seit C++11)
template < class Alloc >
priority_queue ( priority_queue && other, const Alloc & alloc ) ;
(15) (seit C++11)
template < class InputIt, class Alloc >
priority_queue ( InputIt first, InputIt last, const Alloc & alloc ) ;
(16) (seit C++11)
template < class InputIt, class Alloc >

priority_queue ( InputIt first, InputIt last, const Compare & compare,

const Alloc & alloc ) ;
(17) (seit C++11)
template < class InputIt, class Alloc >

priority_queue ( InputIt first, InputIt last, const Compare & compare,

const Container & cont, const Alloc & alloc ) ;
(18) (seit C++11)
template < class InputIt, class Alloc >

priority_queue ( InputIt first, InputIt last, const Compare & compare,

Container && cont, const Alloc & alloc ) ;
(19) (seit C++11)
template < container-compatible-range < T > R >

priority_queue ( std:: from_range_t , R && rg,

const Compare & compare = Compare ( ) ) ;
(20) (seit 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) (seit C++23)
template < container-compatible-range < T > R, class Alloc >
priority_queue ( std:: from_range_t , R && rg, const Alloc & alloc ) ;
(22) (seit C++23)

Konstruiert einen neuen zugrundeliegenden Container des Container-Adapters aus einer Vielzahl von Datenquellen.

1) Standardkonstruktor. Wertinitialisiert den Komparator und den zugrundeliegenden Container.
2) Kopierkonstruiert das Vergleichsfunktor comp mit dem Inhalt von compare . Wertinitialisiert den zugrundeliegenden Container c .
3) Kopierkonstruiert den zugrundeliegenden Container c mit den Inhalten von cont . Kopierkonstruiert das Vergleichsfunktor comp mit den Inhalten von compare . Ruft std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) auf. Dies ist ebenfalls der Standardkonstruktor. (until C++11)
4) Move-konstruiert den zugrundeliegenden Container c mit std :: move ( cont ) . Kopierkonstruiert den Vergleichsfunktor comp mit compare . Ruft std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) auf.
5) Copy-Konstruktor . Der zugrundeliegende Container wird kopierkonstruiert mit other. c . Das Vergleichsfunktor wird kopierkonstruiert mit other. comp . (implizit deklariert)
6) Move-Konstruktor . Der zugrunde liegende Container wird mit std :: move ( other. c ) konstruiert. Das Vergleichsfunktor wird mit std :: move ( other. comp ) konstruiert. (implizit deklariert)
7-9) Iterator-Paar-Konstruktoren. Diese Überladungen nehmen nur dann an der Überladungsauflösung teil, wenn InputIt die Anforderungen an LegacyInputIterator erfüllt.
7) Konstruiert c als ob durch c ( first, last ) und comp von compare . Ruft dann std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) ; auf.
8) Kopierkonstruiert c aus cont und comp aus compare . Ruft dann c. insert ( c. end ( ) , first, last ) ; auf, und anschließend std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) ; .
9) Move-konstruiert c aus std :: move ( cont ) und copy-konstruiert comp aus compare . Ruft dann c. insert ( c. end ( ) , first, last ) ; auf, und ruft anschließend std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) ; auf.
10-15) Allocator-erweiterte Konstruktoren. Diese Überladungen nehmen nur dann an der Überladungsauflösung teil, wenn std:: uses_allocator < container_type, Alloc > :: value gleich true ist, das heißt, wenn der zugrundeliegende Container ein allocator-aware Container ist (für alle Standardbibliothekscontainer zutreffend).
10) Konstruiert den zugrundeliegenden Container mit alloc als Allokator. Ruft effektiv c ( alloc ) auf. comp wird wertinitialisiert.
11) Konstruiert den zugrundeliegenden Container mit alloc als Allokator. Ruft effektiv c ( alloc ) auf. Kopierkonstruiert comp aus compare .
12) Konstruiert den zugrundeliegenden Container mit dem Inhalt von cont und verwendet alloc als Allokator, wie durch c ( cont, alloc ) . Kopierkonstruiert comp aus compare . Ruft dann std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) auf.
13) Konstruiert den zugrundeliegenden Container mit dem Inhalt von cont unter Verwendung von Move-Semantik, während alloc als Allokator verwendet wird, wie durch c ( std :: move ( cont ) , alloc ) . Kopierkonstruiert comp aus compare . Ruft dann std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) auf.
14) Konstruiert den zugrundeliegenden Container mit dem Inhalt von other. c und verwendet alloc als Allokator. Ruft effektiv c ( other. c , alloc ) auf. Kopierkonstruiert comp von other. comp .
15) Konstruiert den zugrundeliegenden Container mit dem Inhalt von other unter Verwendung von Move-Semantik und nutzt dabei alloc als Allokator. Ruft effektiv c ( std :: move ( other. c ) , alloc ) auf. Move-konstruiert comp aus other. comp .
16-19) Allocator-erweiterte Iterator-Paar-Konstruktoren. Identisch mit (7-9) , außer dass alloc für die Konstruktion des zugrundeliegenden Containers verwendet wird. Diese Überladungen nehmen nur dann an der Überladungsauflösung teil, wenn std:: uses_allocator < container_type, Alloc > :: value gleich true ist und InputIt die Anforderungen eines LegacyInputIterator erfüllt.
20) Initialisiert comp mit compare und c mit ranges:: to < Container > ( std:: forward < R > ( rg ) ) . Ruft dann std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) auf.
21) Initialisiert comp mit compare und c mit ranges:: to < Container > ( std:: forward < R > ( rg ) , alloc ) . Ruft dann std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) auf.
22) Initialisiert c mit ranges:: to < Container > ( std:: forward < R > ( rg ) , alloc ) . Ruft dann std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) auf.

Beachten Sie, dass die Art und Weise, wie eine Implementierung prüft, ob ein Typ die Anforderungen eines LegacyInputIterator erfüllt, nicht spezifiziert ist, außer dass Integraltypen abgelehnt werden müssen.

Inhaltsverzeichnis

Parameter

alloc - Allokator, der für alle Speicherallokationen des zugrundeliegenden Containers verwendet wird
other - ein weiterer Container-Adapter, der als Quelle zur Initialisierung des zugrundeliegenden Containers dient
cont - Container, der als Quelle zur Initialisierung des zugrundeliegenden Containers verwendet wird
compare - das Vergleichsfunktionsobjekt zur Initialisierung des zugrundeliegenden Vergleichsfunktors
first, last - das Iteratorpaar, das den Bereich der zu initialisierenden Elemente definiert
rg - ein container-kompatibler Bereich , also ein input_range , dessen Elemente in T konvertierbar sind
Typanforderungen
-
Alloc muss die Anforderungen von Allocator erfüllen.
-
Compare muss die Anforderungen von Compare erfüllen.
-
Container muss die Anforderungen von Container erfüllen. Die Allokator-erweiterten Konstruktoren sind nur definiert, wenn Container die Anforderungen von AllocatorAwareContainer erfüllt.
-
InputIt muss die Anforderungen von LegacyInputIterator erfüllen.

Komplexität

1,2) Konstante.
3,5,12) O(N) Vergleiche und O(N) Aufrufe des Konstruktors von value_type , wobei N cont. size ( ) ist.
4) O(N) Vergleiche, wobei N gleich cont. size ( ) ist.
6) Konstante.
7,16,17) O(M) Vergleiche, wobei M gleich std:: distance ( first, last ) ist.
8,18) O(N + M) Vergleiche und O(N) Aufrufe des Konstruktors von value_type , wobei N gleich cont. size ( ) und M gleich std:: distance ( first, last ) ist.
9) O(N + M) Vergleiche, wobei N die cont. size ( ) und M die std:: distance ( first, last ) ist.
10,11) Konstante.
13) O(N) Vergleiche, wobei N gleich cont. size ( ) ist.
14) Linear in der Größe von other .
15) Konstant, falls Alloc gleich dem Allokator von other ist. Linear in der Größe von other andernfalls.
19) O(N + M) Vergleiche und möglicherweise O(N) Aufrufe des Konstruktors von value_type (vorhanden falls Alloc nicht gleich dem Allokator von other ist), wobei N gleich cont. size ( ) und M gleich std:: distance ( first, last ) ist.
20) O(N) Vergleiche und O(N) Aufrufe des Konstruktors von value_type , wobei N gleich ranges:: distance ( rg ) ist.
21,22)

Hinweise

Feature-Test Makro Wert Std Feature
__cpp_lib_containers_ranges 202202L (C++23) Ranges-bewusste Konstruktion und Einfügung; Überladungen ( 20-22 )

Beispiel

#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
}

Ausgabe:

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)

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
P0935R0 C++11 Standardkonstruktor und Konstruktor (4) waren explizit implizit gemacht
LWG 3506 C++11 Allokator-erweiterte Iterator-Paar-Konstruktoren fehlten hinzugefügt
LWG 3522 C++11 Einschränkungen für Iterator-Paar-Konstruktoren fehlten hinzugefügt
LWG 3529 C++11 Konstruktion aus einem Iterator-Paar rief insert auf konstruiert den Container daraus

Siehe auch

weist Werte dem Container-Adapter zu
(öffentliche Elementfunktion)