この記事では、Visual C++ で STL 関数の priority_queue::push、 priority_queue::pop、 priority_queue::empty、 priority_queue::top、 priority_queue::size を使用する方法に関する問題を解決するのに役立ちます。
元の製品バージョン: Visual C++
元の KB 番号: 157623
まとめ
次のサンプル コードは、Visual C++ で STL 関数の priority_queue::push、 priority_queue::pop、 priority_queue::empty、 priority_queue::top、 priority_queue::size を使用する方法を示しています。
priority_queue アダプターは、priority_queueでサポートされているコンテナーの種類によって定義された型のオブジェクトを保持します。 サポートされている 2 つのコンテナーは、 vector と dequeです。 オブジェクトは push() によって挿入され、 pop()によって削除されます。 top() は、 priority_queueの一番上の項目を返します。
アダプターは反復処理をサポートしていないため、 priority_queue には反復子が関連付けされていません。
Priority_queue を使用すると、関連する比較子関数によって決定されるアイテムの並べ替えられたコレクション (less、greater など) を維持できます。したがって、上位の項目は、選択した関数に基づいて最も低い、または最高の選択肢になります。
必須ヘッダー
<queue>
プロトタイプ
priority_queue::push();
priority_queue::pop();
priority_queue::empty();
priority_queue::top();
priority_queue::size();
Note
プロトタイプのクラス名またはパラメーター名が、ヘッダー ファイルのバージョンと一致しない可能性があります。 読みやすさを向上させるために Sme が変更されました。
サンプル コード
このサンプルは、dequeコンテナーとvector コンテナーを使用したpriority_queue実装を示しています。
//////////////////////////////////////////////////////////////////////
// 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