Verwenden von STL-Funktionen priority_queue::(push, pop, empty, top) in Visual C++

Dieser Artikel hilft Ihnen bei der Behebung des Problems, bei dem die priority_queue::pushFunktionen , priority_queue::pop, , priority_queue::empty``priority_queue::topund priority_queue::size STL in Visual C++ verwendet werden.

Originalversion des Produkts:   Visual C++
Ursprüngliche KB-Nummer:   157623

Zusammenfassung

Der folgende Beispielcode veranschaulicht die Verwendung der priority_queue::pushFunktionen , priority_queue::pop, priority_queue::empty, priority_queue::topund priority_queue::size STL in Visual C++.

Der priority_queue Adapter enthält Objekte des Typs, der durch den containertyp definiert wird, der priority_queuevom unterstützt wird. Die beiden unterstützten Container sind der vector und der deque. Objekte werden von push() eingefügt und von pop()entfernt. top() gibt das oberste Element in der priority_queuezurück.

Da Adapter die Iteration nicht unterstützen, verfügt ein priority_queue Adapter über keinen zugeordneten Iterator.

Priority_queue ermöglicht es Ihnen, eine sortierte Sammlung von Elementen zu verwalten, die durch eine zugeordnete Vergleichsfunktion bestimmt wird, z. B. weniger, größer usw. Das oberste Element wird daher zum Kandidaten der Wahl, zum niedrigsten oder höchsten Element basierend auf der gewählten Funktion.

Erforderlicher Header

<queue>

Prototyp

priority_queue::push();
priority_queue::pop();
priority_queue::empty();
priority_queue::top();
priority_queue::size();

Hinweis

Die Klassen-/Parameternamen im Prototyp stimmen möglicherweise nicht mit der Version in der Headerdatei überein. Kmu wurden geändert, um die Lesbarkeit zu verbessern.

Beispielcode

Das Beispiel zeigt die priority_queueImplementierung mithilfe deque von Containern.vector

//////////////////////////////////////////////////////////////////////
// Compile options needed: /GX
// <filename> : priority_queue.cpp
// Functions:
// priority_queue::push(), priority_queue::pop(),
// priority_queue::empty(), priority_queue::top(), queue::size()
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <functional>
using namespace std;

#if _MSC_VER > 1020 // if VC++ version is > 4.2
    using namespace std; // std c++ libs implemented in std
#endif

// Using priority_queue with deque
// Use of function greater sorts the items in ascending order
typedef deque<int, allocator<int> > INTDQU;
typedef priority_queue<int,INTDQU, greater<int> > INTPRQUE;

// Using priority_queue with vector
// Use of function less sorts the items in descending order
typedef vector<char, allocator<char> > CHVECTOR;
typedef priority_queue<char,CHVECTOR,less<char> > CHPRQUE;

void main(void)
{
    int size_q;
    INTPRQUE q;
    CHPRQUE p;

    // Insert items in the priority_queue(uses deque)
    q.push(42);
    q.push(100);
    q.push(49);
    q.push(201);

    // Output the item at the top using top()
    cout << q.top() << endl;
    // Output the size of priority_queue
    size_q = q.size();
    cout << "size of q is:" << size_q << endl;
    // Output items in priority_queue using top()
    // and use pop() to get to next item until
    // priority_queue is empty
    while (!q.empty())
    {
        cout << q.top() << endl;
        q.pop();
    }

    // Insert items in the priority_queue(uses vector)
    p.push('c');
    p.push('a');
    p.push('d');
    p.push('m');
    p.push('h');

    // Output the item at the top using top()
    cout << p.top() << endl;

    // Output the size of priority_queue
    size_q = p.size();
    cout << "size of p is:" << size_q << endl;

    // Output items in `priority_queue`using top()
    // and use pop() to get to next item until
    // `priority_queue`is empty
    while (!p.empty())
    {
        cout << p.top() << endl;
        p.pop();
    }
}

Programmausgabe:

4
size of q is:4
42
49
100
201
m
size of p is:5
m
h
d
c
a