优先队列
#include <queue>
using namespace std;
priority_queue<元素类型[[,底层容器类型],比较器类型]>
优先队列对象 (构造实参表);
底层容器:vector/deque(默认)
优者先出,默认以大者为优,也可以通过比较器定制优先级。
首------->尾
50 45 40 30
#include <iostream> #include <queue> #include <vector> using namespace std; class IntCmp { public: bool operator() (int a, int b) const{ return a > b; } }; class Integer { public: Integer (int arg) : m_var (arg) {} bool operator< ( Integer const& i) const { // return m_var < i.m_var; return m_var > i.m_var; } friend ostream& operator<< ( ostream& os, Integer const& i) { return os << i.m_var; } private: int m_var; }; int main (void) { // priority_queue<int> pq; // priority_queue<int, vector<int>, // IntCmp> pq; priority_queue<Integer> pq; pq.push (40); pq.push (50); pq.push (30); pq.push (60); pq.push (20); pq.push (60); pq.push (40); while (! pq.empty ()) { cout << pq.top () << ' ' <<flush; pq.pop (); } cout << endl; return 0; }
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
· C# 锁机制全景与高效实践:从 Monitor 到 .NET 9 全新 Lock
· 一则复杂 SQL 改写后有感
· golang中写个字符串遍历谁不会?且看我如何提升 50 倍
· 突发,CSDN 崩了!程序员们开始慌了?
· 完成微博外链备案,微博中直接可以打开园子的链接
· C# WinForms 实现打印监听组件
· 推荐 3 种 .NET Windows 桌面应用程序自动更新解决方案
· 一个基于 .NET 开源、模块化 AI 图像生成 Web 用户界面