pair作为priority_queue元素时,自定义排序算法

本文介绍了C++中的priority_queue及其使用场景,重点讲解了如何通过pair结构配合less和greater函数实现自定义排序,并通过实例演示了降序和升序排列。同时,讨论了pair的基本概念和make_pair的用法,以及pair在优先队列中的默认排序规则。

priority_queue介绍

定义:priority_queue<Type, Container, Functional>

Type 就是数据类型。(int ,double等.)

Container 就是容器类型(Container必须是用数组实现的容器,默认vector,但不能用 list。)

默认降序排列,push()队尾插入,pop()弹出头元素。

priority_queue<int> a; //降序排列 等同于priority_queue<int,vector<int>,less<int>> b;
priority_queue<int,vector<int>,greater<int>> c; //升序排列

less表示数字大的优先级越大
greater表示数字小的优先级越大

pair 介绍

pair将两个数据组合成一组数据。
pair的实现是一个结构体,主要的两个成员变量是first、second。

pair<string,string> anon;
pair<string,int> word_count;
pair<string,int> name_age("Tom",18); //初始化
  • 使用make_pair
pair<int,double> p1;
p1 = make_pair(1,1,2);
  • 获取元素
p1.first;
p1.second;
  • pair比较大小

先按first比较,如果相等,再按照second比较。

默认排序

pair作为priority_queue元素时,使用默认less进行降序排列。

#include<iostream>
#include<string>
#include<functional>
#include<queue>
#include<vector>

using namespace std;
typedef pair<int,int> pairint;

int main()
{
        priority_queue<pairint,vector<pairint>,less<pairint> > test;
        pair<int,int> a(5,3);
        pair<int,int> b(1,10);
        pair<int,int> c(5,1);

        test.push(a);
        test.push(b);
        test.push(c);

        while (!test.empty()) {
                cout<<test.top().first<<","<<test.top().second<<endl;
                test.pop();
        }

        return 0;
}                       

输出:

5,3
5,1
1,10

当pair的first值相同时,比较second。

自定义排序顺序

通过自定义function object实现优先队列排序。

#include<iostream>
#include<string>
#include<functional>
#include<queue>
#include<vector>

using namespace std;
typedef pair<int,string> pairint;

class Comp{
        public:
        bool operator()(const pairint &n1,const pairint &n2)const
        {
                if(n1.first == n2.first)
                        return n1.first > n2.first;
                return n1.first > n2.first;
        }
};

int main()
{
        priority_queue<pairint,vector<pairint>,Comp > test;
        pair<int,string> a(5,"a");
        pair<int,string> b(1,"b");
        pair<int,string> c(5,"c");

        test.push(a);
        test.push(b);
        test.push(c);

        while (!test.empty()) {
                cout<<test.top().first<<","<<test.top().second<<endl;
                test.pop();
        }

        return 0;
}

输出:

1,b
5,a
5,c
在 C++ 中,`priority_queue` 是一种容器适配器,可以用于实现最大堆或最小堆。当使用 `pair<int, int>` 类型,默认情况下,`priority_queue` 会根据 `pair` 的第一个元素进行排序,如果第一个元素相同,则比较第二个元素。 ### 声明和初始化 以下是一个基本的声明方式: ```cpp #include <iostream> #include <queue> #include <vector> #include <utility> // for std::pair int main() { // 默认是最大堆,基于 pair 的字典序比较 std::priority_queue<std::pair<int, int>> pq; // 插入元素 pq.push({1, 10}); pq.push({3, 30}); pq.push({2, 20}); // 输出并移除堆顶元素 while (!pq.empty()) { std::cout << "Top element: (" << pq.top().first << ", " << pq.top().second << ")" << std::endl; pq.pop(); } return 0; } ``` 在这个例子中,`priority_queue` 使用默认的比较方式,即按照 `pair` 的 **字典序** 进行排序。输出顺序为 `(3, 30)`、`(2, 20)`、`(1, 10)`,因为默认是最大堆。 ### 自定义排序规则 如果你希望按照不同的规则进行排序(例如,使最小的第一个元素优先),则需要提供一个自定义的比较器[^3]。例如,构建一个最小堆: ```cpp #include <iostream> #include <queue> #include <vector> #include <utility> // 自定义比较函数:最小堆,按第一个元素排序 struct ComparePair { bool operator()(const std::pair<int, int>& a, const std::pair<int, int>& b) { return a.first > b.first; // 最小堆 } }; int main() { // 使用自定义比较器的 priority_queue std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, ComparePair> pq; // 插入元素 pq.push({1, 10}); pq.push({3, 30}); pq.push({2, 20}); // 输出并移除堆顶元素 while (!pq.empty()) { std::cout << "Top element: (" << pq.top().first << ", " << pq.top().second << ")" << std::endl; pq.pop(); } return 0; } ``` 此代码将按照 `pair` 的第一个元素升序排列,输出顺序为 `(1, 10)`、`(2, 20)`、`(3, 30)`。 ### 常见用途 `priority_queue<pair<int, int>>` 常用于需要同处理两个相关数据的情况,例如: - 图算法中的节点权重表示(如 Dijkstra 算法)。 - 滑动窗口问题中的索引与值组合[^4]。 - 多条件排序任务调度等场景。 通过灵活地使用默认比较规则或自定义比较器,`priority_queue` 可以适应多种应用场景。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

硬码农二毛哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值