優先隊列(priority_queue)C++詳解

頭文件#inclue<queue>
我們可以定義其中數據的優先級,讓優先級高的排在隊列前面,優先出隊

優先隊列具有隊列的所有特性,包括基本操作,但是優先隊列在隊列的基礎上添加了一個內部的排序功能,本質是小頂堆和大頂堆實現的

基本操作
和隊列基本操作相同:
top :訪問隊頭元素
empty :隊列是否爲空
size :返回隊列內元素個數
push :插入元素到隊尾 (並排序)
emplace :原地構造一個元素並插入隊列
pop :彈出隊頭元素
swap :交換內容

對於隊列的使用可參考:隊列詳解

定義

priority_queue<Type,Container,Functional>

Type 就是數據類型
Container 就是容器類型(Container必須是用數組實現的容器,比如vector,deque等等,但不能用 list。STL裏面默認用的是vector)
Functional 就是比較的方式,當需要用自定義的數據類型時才需要傳入這三個參數,使用基本數據類型時,只需要傳入數據類型,默認是大頂堆

一般爲:

//升序隊列(小頂堆)
priority_queue <int,vector<int>,greater<int> > q;
//降序隊列(大頂堆)
priority_queue <int,vector<int>,less<int> >q;

//greater和less是std實現的兩個仿函數(就是使一個類的使用看上去像一個函數。其實現就是類中實現一個operator(),這個類就有了類似函數的行爲,就是一個仿函數類了)

1.基本類型運用示例

#include<iostream>
#include<queue>
#include<string>
using namespace std;

int main(){
	/**********************測試默認大頂堆*******************/
	//這裏一定要有空格,不然成了右移運算符↓
	priority_queue<int> qu;//默認大頂堆 
	//相當於priority_queue<int,vector<int>,greater<int> > qu; 
	for(int i=0;i<=10;i++){
		qu.push(i);
	}
	cout<<"測試默認大頂堆:" ;
	while(!qu.empty()){
		cout<<qu.top()<<" ";
		qu.pop();
	} 
	
	
	/**********************測試大頂堆*******************/
	priority_queue<int,vector<int>,less<int> > qmax;//定義大頂堆 
	for(int i=0;i<=10;i++){
		qmax.push(i);
	}
	cout<<endl<<"測試大頂堆:";
	while(!qmax.empty()){
		cout<<qmax.top()<<" ";
		qmax.pop();
	} 
	
	
	/**********************測試小頂堆*******************/
	priority_queue<int,vector<int>,greater<int> > qmin;//定義小頂堆 
	for(int i=10;i>=0;i--){
		qmin.push(i);
	}
	cout<<endl<<"測試小頂堆:";
	while(!qmin.empty()){
		cout<<qmin.top()<<" ";
		qmin.pop();
	} 
	
	/**********************試字符串小頂堆*******************/	
	priority_queue<string,vector<string>,greater<string> > qsmix;//定義小頂堆
	cout<<endl<<"測試字符串小頂堆:";
	qsmix.push("abc");
	qsmix.push("bbc");
	qsmix.push("acc");
	while(!qsmix.empty()){
		cout<<qsmix.top()<<" ";
		qsmix.pop();
	}
	return 0;
} 

結果:
在這裏插入圖片描述
2.pari的比較,先比較第一個元素,第一個相等比較第二個

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() 
{
    priority_queue<pair<int, int> > a;
    pair<int, int> b(1, 2);
    pair<int, int> c(1, 3);
    pair<int, int> d(2, 5);
    a.push(d);
    a.push(c);
    a.push(b);
    while (!a.empty()) 
    {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }
}

結果:
在這裏插入圖片描述
3.對於自定義類型

#include <iostream>
#include <queue>
using namespace std;

//方法1
struct tmp1 //運算符重載<
{
    int x;
    tmp1(int a) {x = a;}
    bool operator<(const tmp1& a) const
    {
        return x < a.x; //大頂堆
    }
};

//方法2
struct tmp2 //重寫仿函數
{
    bool operator() (tmp1 a, tmp1 b) 
    {
        return a.x < b.x; //大頂堆
    }
};

int main() 
{
    tmp1 a(1);
    tmp1 b(2);
    tmp1 c(3);
    priority_queue<tmp1> d;
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty()) 
    {
        cout << d.top().x << '\n';
        d.pop();
    }
    cout << endl;

    priority_queue<tmp1, vector<tmp1>, tmp2> f;
    f.push(c);
    f.push(b);
    f.push(a);
    while (!f.empty()) 
    {
        cout << f.top().x << '\n';
        f.pop();
    }
}

結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章