跳到正文

个人记录

10月4日上午 优先队列

mc__bang约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年10月4日
收录
2025年12月12日
更新
2026年11月5日
篇幅
约 1 分钟

版本

  1. v1当前

优先队列

大根堆

普通数据类型

定义:

C++
priority_queue<int>q;

插入:

C++
q.push(x);

删除:

C++
q.pop();

获取堆顶

C++
q.top();

自定义数据类型使用大根堆

结构体定义:

C++
struct node
{
   int score;
   bool operator <(const node& w)const{//重写小于号
       return score < w.score;
   }
};
priority_queue<node>q;//定义一个node类型的大根堆

小根堆

普通数据类型

定义:

C++
priority_queue<int, vector<int>, greater<int> >q;//greater<int>是比较器

自定义数据类型使用小根堆

结构体定义:

C++
struct node
{
   int score;
   bool operator <(const node& w)const{//重写大于号
       return score > w.score;
   }
};
priority_queue<node>q;//定义一个node类型的小根堆
原文