【C++ STL】priority_queue自定义排序函数

priority_queue是C++的一种STL容器,实现为堆。在leetcode刷题中非常常用。有些时候我们需要塞入自定义的数据结构。这样就需要对其的排序方式做一个重新定义。
假设有以下数据结构

struct node {
	int x;
	int y;
};

对比方式为只看x的大小。把x的值大的放在堆顶。
则对比函数cmp写法如下:

1. 仿函数(函数对象)

用这种方式,需要显示的定义优先队列的容器类型(vector)以及比较函数(cmp)。

#include<iostream>
#include<queue>
using namespace std;
//函数对象类
template <typename T>
class cmp
{
public:
    //重载 () 运算符
    bool operator()(T a, T b)
    {
        return a.x < b.x;
    }
};
struct node {
    int x;
    int y;
};
int main()
{
    node a = {1,2};
    node b = {0,2};
    node c = {1,3};
    node d = {2,5};
    node e = {3,6};
    priority_queue<node, vector<node>, cmp<node>> pq; // 显式指定容器类型和比较函数
    pq.push(a);
    pq.push(b);
    pq.push(c);
    pq.push(d);
    pq.push(e);
    while (!pq.empty())
    {
        cout << pq.top().x << "," << pq.top().y << endl;
        pq.pop();
    }
    return 0;
}

结果:
在这里插入图片描述

2. 使用自定义类型比较关系

重载需要比较类型的 < < <符号,之后在类型直接写类型名称即可。

#include<iostream>
#include<queue>
using namespace std;
struct node {
    int x;
    int y;
    bool operator < (node b) const { // 这里后面的const必须加
        return this->x < b.x;
    }
};
int main()
{
    node a = {1,2};
    node b = {0,2};
    node c = {1,3};
    node d = {2,5};
    node e = {3,6};
    priority_queue<node> pq; // 只写类型名称即可
    pq.push(a);
    pq.push(b);
    pq.push(c);
    pq.push(d);
    pq.push(e);
    while (!pq.empty())
    {
        cout << pq.top().x << "," << pq.top().y << endl;
        pq.pop();
    }
    return 0;
}

结果:
在这里插入图片描述

3. 使用lambda表达式

使用lambda表达式需要在pq对象构造的时候,将lambda表达式作为参数传入其中。即pq(cmp)

#include<iostream>
#include<queue>
#include<functional>
using namespace std;

struct node {
    int x;
    int y;
};
int main()
{
    auto cmp = [](node x, node y) {
        return x.x < y.x;
    };
    node a = {1,2};
    node b = {0,2};
    node c = {1,3};
    node d = {2,5};
    node e = {3,6};
    priority_queue<node, vector<node>, decltype(cmp)> pq(cmp); // 需要在pq对象创建的时候将lambda表达式作为参数传入
    // priority_queue<node, vector<node>, function<bool(node, node)> > pq(cmp); // 和上面的效果相同,但是用function需要包含头文件functional
    pq.push(a);
    pq.push(b);
    pq.push(c);
    pq.push(d);
    pq.push(e);
    while (!pq.empty())
    {
        cout << pq.top().x << "," << pq.top().y << endl;
        pq.pop();
    }
    return 0;
}

结果:
在这里插入图片描述

4. 函数指针(和上面的相比有些多余,刷题不是很实用)

这种方式和lambda表达式的方式很类似。

#include<iostream>
#include<queue>
#include<functional>
using namespace std;

struct node {
    int x;
    int y;
};
bool cmp(node x, node y)
{
    return x.x < y.x;
}
int main()
{
    node a = {1,2};
    node b = {0,2};
    node c = {1,3};
    node d = {2,5};
    node e = {3,6};
    bool (*funcp)(node x, node y) = cmp;
    priority_queue<node, vector<node>, decltype(*funcp)> pq(*funcp);
    pq.push(a);
    pq.push(b);
    pq.push(c);
    pq.push(d);
    pq.push(e);
    while (!pq.empty())
    {
        cout << pq.top().x << "," << pq.top().y << endl;
        pq.pop();
    }
    return 0;
}

结果:
在这里插入图片描述
reference:

https://blog.csdn.net/Strengthennn/article/details/119078911

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STL中的priority_queue是一个优先队列,它是一个使用堆来实现的容器。它可以按照一定的优先级顺序存储元素,并且每次访问队首元素都是访问优先级最高的元素。 在使用priority_queue时,可以通过定义不同的比较函数来指定元素的优先级顺序。默认情况下,对于基本类型,默认是大顶堆,降序队列。也可以通过指定参数来实现小顶堆,升序队列。例如: priority_queue<int, vector<int>, greater<int>> q; //小顶堆,升序队列 priority_queue<int, vector<int>, less<int>> q; //大顶堆,降序队列 在对priority_queue进行操作时,可以使用push()函数向队列中插入元素,使用top()函数获取队首元素,使用pop()函数删除队首元素。 在自定义类型的优先队列中,可以重载运算符>或<来定义优先级。例如,可以重载operator>来定义小顶堆,即优先级较小的元素排在前面。示例代码如下: struct Node{ int x, y; Node(int a=0, int b=0): x(a), y(b) {} }; bool operator> (Node a, Node b){ if(a.x == b.x) return a.y > b.y; return a.x > b.x; } priority_queue<Node, vector<Node>, greater<Node>> q; q.push(Node(rand(), rand())); while(!q.empty()){ cout<<q.top().x<<' '<<q.top().y<<endl; q.pop(); } 总结来说,C++ STL中的priority_queue是一个使用堆实现的优先队列,可以按照指定的优先级顺序存储元素。可以通过定义不同的比较函数或重载运算符来指定优先级规则。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【总结】C++ 基础数据结构 —— STL之优先队列(priority_queue) 用法详解](https://blog.csdn.net/weixin_44668898/article/details/102132580)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值