優先佇列詳解

2021-08-18 18:55:04 字數 1038 閱讀 5841

在優先佇列中,優先順序高的元素優先出列,複雜度為log(n)。

empty

true if the priority queue has no elements

popremoves 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

topreturns the top element of the priority queue

這是我們最常用的方法,標註庫函式中預設大的元素優先順序高排列,**如下:

priority_queueq;
很多時候,我們也需要從優先對小的元素進行操作,這時候需要我們傳入乙個比較函式,**如下:

#include //要傳入比較函式,這個標頭檔案不能忘記

priority_queue,greater>q;//小的元素優先順序高

priority_queue,less>q;//大的元素優先順序高

第三種方法:

其實和第二種方法一樣,只不過是自定義比較函式,**如下:

struct cmp1

};struct cmp2

};priority_queue,cmp1> q;

priority_queue,cmp2> q;

主要是對第三種方法的拓展,對於結構體變數的多優先順序,**如下:

struct node

int x;//第一優先順序

int y;//第二優先順序

}node[n];

struct node

int x;//第一優先順序

int y;//第二優先順序

}node[n];

priority_queueq;

優先佇列詳解

優先佇列具有佇列的所有特性,包括基本操作,只是在這基礎上新增了內部的乙個排序,它本質是乙個堆實現的 定義 priority queue type 就是資料型別,container 就是容器型別 container必須是用陣列實現的容器,比如vector,deque等等,但不能用 list。stl裡面...

STL優先佇列詳解

優先佇列是一種抽象資料型別 abstract date type,adt 行為和佇列類似,但是先出隊的元素不是先進佇列的元素,而是佇列中優先順序最高的元素。stl的優先佇列定義在標頭檔案和 佇列一樣 用 priority queuepq 來宣告 最基本的用法 定義 priority queuepq ...

優先佇列priority queue詳解

include using namespace std 比如 priority queue i priority queue d priority queue q node是乙個結構體 結構體裡過載了 小於符號 priority queue greater q 不需要 include標頭檔案 注意後...