這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)priority_queue怎么在c++中使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

目前創(chuàng)新互聯(lián)已為數(shù)千家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)頁空間、綿陽服務器托管、企業(yè)網(wǎng)站設計、瑪曲網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
首先要包含頭文件#include<queue>, 他和queue不同的就在于我們可以自定義其中數(shù)據(jù)的優(yōu)先級, 讓優(yōu)先級高的排在隊列前面,優(yōu)先出隊。
優(yōu)先隊列具有隊列的所有特性,包括隊列的基本操作,只是在這基礎上添加了內(nèi)部的一個排序,它本質(zhì)是一個堆實現(xiàn)的。
和隊列基本操作相同:
top 訪問隊頭元素
empty 隊列是否為空
size 返回隊列內(nèi)元素個數(shù)
push 插入元素到隊尾 (并排序)
emplace 原地構(gòu)造一個元素并插入隊列
pop 彈出隊頭元素
swap 交換內(nèi)容
定義:priority_queue<Type, Container, Functional>
Type 就是數(shù)據(jù)類型,Container 就是容器類型(Container必須是用數(shù)組實現(xiàn)的容器,比如vector,deque等等,但不能用 list。STL里面默認用的是vector),F(xiàn)unctional 就是比較的方式。
當需要用自定義的數(shù)據(jù)類型時才需要傳入這三個參數(shù),使用基本數(shù)據(jù)類型時,只需要傳入數(shù)據(jù)類型,默認是大頂堆。
一般是:
//升序隊列 priority_queue <int,vector<int>,greater<int> > q; //降序隊列 priority_queue <int,vector<int>,less<int> >q; //greater和less是std實現(xiàn)的兩個仿函數(shù)(就是使一個類的使用看上去像一個函數(shù)。其實現(xiàn)就是類中實現(xiàn)一個operator(),這個類就有了類似函數(shù)的行為,就是一個仿函數(shù)類了)
1、基本類型優(yōu)先隊列的例子:
#include<iostream>
#include <queue>
using namespace std;
int main()
{
//對于基礎類型 默認是大頂堆
priority_queue<int> a;
//等同于 priority_queue<int, vector<int>, less<int> > a;
// 這里一定要有空格,不然成了右移運算符↓↓
priority_queue<int, vector<int>, greater<int> > c; //這樣就是小頂堆
priority_queue<string> b;
for (int i = 0; i < 5; i++)
{
a.push(i);
c.push(i);
}
while (!a.empty())
{
cout << a.top() << ' ';
a.pop();
}
cout << endl;
while (!c.empty())
{
cout << c.top() << ' ';
c.pop();
}
cout << endl;
b.push("abc");
b.push("abcd");
b.push("cbd");
while (!b.empty())
{
cout << b.top() << ' ';
b.pop();
}
cout << endl;
return 0;
}運行結(jié)果:
4 3 2 1 0
0 1 2 3 4
cbd abcd abc
請按任意鍵繼續(xù). . .
2、用pair做優(yōu)先隊列元素的例子:
規(guī)則:pair的比較,先比較第一個元素,第一個相等比較第二個。
#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();
}
}運行結(jié)果:
2 5
1 3
1 2
請按任意鍵繼續(xù). . .
3、用自定義類型做優(yōu)先隊列元素的例子
#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 //重寫仿函數(shù)
{
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(b);
f.push(c);
f.push(a);
while (!f.empty())
{
cout << f.top().x << '\n';
f.pop();
}
}運行結(jié)果:
3
2
1
3
2
1
上述就是小編為大家分享的priority_queue怎么在c++中使用了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱:priority_queue怎么在c++中使用
當前URL:http://chinadenli.net/article18/jpssgp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計公司、微信公眾號、網(wǎng)頁設計公司、網(wǎng)站建設、、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)