共用方式為


在 Visual C++中使用 priority_queue::(push、pop、empty、top) STL 函式

本文可協助您解決如何在 Visual C++中使用 priority_queue::pushpriority_queue::poppriority_queue::emptypriority_queue::top、 和 priority_queue::size STL 函式的問題。

原始產品版本: Visual C++
原始 KB 編號: 157623

摘要

下列範例程式代碼說明如何在 Visual C++中使用 priority_queue::pushpriority_queue::poppriority_queue::emptypriority_queue::toppriority_queue::size STL 函式。

priority_queue 接器會保存 由 所支援 priority_queue之容器類型所定義的型別物件。 支援的兩個容器是 vectordeque。 物件會由 插入 push() ,並由 pop()移除。 top() 會傳回 中的 priority_queue頂端專案。

因為配接器不支援反覆專案,所以 priority_queue 沒有相關聯的反覆運算器。

Priority_queue 可讓您維護由相關聯比較子函式所決定之專案的排序集合,例如較少、更大等。因此,最上層專案會根據所選的函式成為選擇範圍、最低或最高的候選專案。

必要的標頭

<queue>

原型

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

注意

原型中的類別/參數名稱可能不符合頭檔中的版本。 已修改 Sme 以改善可讀性。

範例指令碼

此範例顯示 priority_queue使用 dequevector 容器的實作。

//////////////////////////////////////////////////////////////////////
// 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();
    }
}

程式輸出:

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