優先キューの使用方法

1581 ワード

先日ハフマンの欲張りをして、自分でシミュレーションしようとしたのは間違っていた.
そこで優先行列を作って水を流しました
いっそまとめる
ヘッダファイル:#include
一:使用する関数
メンバー関数:
empty
true if the priority queue has no elements
pop
removes the top element of a priority queue
push
adds an element to the end of the priority queue
size
returns the number of items in the priority queue
top
returns the top element of the priority queue
よく使うのもこれくらいです
使用方法:
空かどうかを判断します.

if (q.empty()) ...
 

ポップアップ要素:

q.pop();


要素を挿入:

q.push(x);
 

ここでxはqが示す優先キューの要素タイプでなければならない
元のキューをクリアします(特に複数のデータのグループの場合は注意してください):

while(!q.empty()) q.pop();


2:大から小へのソート

priority_queue<int> q;


ここでは変数のタイプであり、任意のタイプであってもよい.
qはqueueキュー名
3:小さい順から大きい順に並べ替える

priority_queue<int,vector<int>,greater<int> > q;


四:カスタムソート
マルチレベル変数のソートには、構造体変数をカスタマイズする必要があります.
struct node{

      int x,y;

      friend bool operator < (node n1,node n2){

           if (n1.x!=n2.x) return n1.x<n1.x;

           return n1.y<n2.y;

    }

}

priority_queue<node> q;


上記の小さい番号のリロードから、nodeではxを第1レベル、yを第2レベルとしてソートされていることがわかります.昇順ですか、降順ですか.
みんなは自分で実験をしましょう.