網站首頁 編程語言 正文
既然是隊列那么先要包含頭文件#include <queue>
, 他和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> 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; }
輸出
4 3 2 1 0 0 1 2 3 4 cbd abcd abc
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(); } }
輸出
2 5 1 3 1 2
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(); } }
輸出
3 2 1 3 2 1
原文鏈接:https://blog.csdn.net/weixin_36888577/article/details/79937886
相關推薦
- 2022-03-29 詳解python的集合set的函數_python
- 2022-08-07 Go?gRPC超時控制Deadlines用法詳解_Golang
- 2022-07-18 windows多客戶端與liunx-ubuntu服務端進行通信
- 2022-08-02 C#中的延時函數sleep_C#教程
- 2022-06-22 android實現注冊登錄程序_Android
- 2022-05-12 Android uniapp項目接入實例、uniapp混合開發踩坑手冊、uniapp Android
- 2022-07-01 c++詳細講解構造函數的拷貝流程_C 語言
- 2022-01-19 解決element-ui 表格分頁序號不遞增問題。
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支