Udostępnij za pośrednictwem


Używanie funkcji STL priority_queue::(push, pop, empty, top) w programie Visual C++

Ten artykuł ułatwia rozwiązanie problemu polegającego priority_queue::pushna tym, jak używać funkcji , , priority_queue::pop, priority_queue::emptypriority_queue::topi priority_queue::size STL w języku Visual C++.

Oryginalna wersja produktu: Visual C++
Oryginalny numer KB: 157623

Podsumowanie

Poniższy przykładowy kod ilustruje sposób używania priority_queue::pushfunkcji , priority_queue::pop, priority_queue::empty, priority_queue::topi priority_queue::size STL w języku Visual C++.

Karta priority_queue przechowuje obiekty typu zdefiniowanego przez typ kontenera obsługiwanego przez obiekt priority_queue. Obsługiwane są dwa kontenery i vector deque. Obiekty są wstawiane push() i usuwane przez pop()element . top() Zwraca górny element w elemencie priority_queue.

Ponieważ karty nie obsługują iteracji, iterator nie ma skojarzonego priority_queue iteratora.

Priority_queue umożliwia utrzymanie posortowanej kolekcji elementów określonych przez skojarzona funkcja komparatora, na przykład mniejsza, większa itp. W związku z tym górny element staje się kandydatem do wyboru, najniższym lub najwyższym na podstawie wybranej funkcji.

Wymagany nagłówek

<queue>

Prototyp

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

Uwaga 16.

Nazwy klas/parametrów w prototypie mogą nie być zgodne z wersją w pliku nagłówka. Sme zostały zmodyfikowane w celu poprawy czytelności.

Przykładowy kod

W przykładzie pokazano priority_queueimplementację przy użyciu deque kontenerów i .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();
    }
}

Dane wyjściowe programu:

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