跳到正文

个人记录

大根堆

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

文章信息

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

版本

  1. v2当前
  2. v1

大根堆

定义方式:

大根堆的定义:

C++
priority_queue<数据类型, 容器类型, 比较函数类型>队列名; //这种定义默认为大根堆

小根堆的定义:

利用**greater<>**作为比较函数

C++
priority_queue<int,vector<int>,greater<int>> min_heap;

常用函数

C++
push(x);//插入一个元素x
pop();//弹出队首元素
top();//获取队首元素
size();//队列中元素的个数

自定义数据类型:

方法一

C++
struct node{
    string s;
    int a;
    bool operator<(const node& x)const{
        return a<x.a;
    }
};
priority_queue<node> n;

方法二

C++
struct sss{
    tring s;
    int x;
};
struct cs{
    bool operator()(const sss& a,const sss& b)const{
        return a.x>b.x;
    }
};
priority_queue<sss,vector<sss>,cs> min_score_heap;
原文