C++容器适配器之priority_queue

定义:priority_queue<Type, Container, Functional>

  • Type就是数据类型
  • Container就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用ist。STL里面默认用的是vector
  • Functional就是比较的方式,当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆

一般是:

//升序队列
priority_queue <int,vector<int>,greater<int> > pq;
//降序队列
priority_queue <int,vector<int>,less<int> >pq;

//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)

1、基本类型

1.1 使用默认仿函数

#include<iostream>
#include <queue>
using namespace std;
int main() 
{
    //对于基础类型 默认是大顶堆
    priority_queue<int> a; 
    //等同于 priority_queue<int, vector<int>, less<int> > a;
    
    // 这里一定要有空格,不然成了右移运算符
    priority_queue<int, vector<int>, greater<int> > c;  //这样就是小顶堆
    priority_queue<string> b;
    priority_queue<pair<int, int> > d;
	//整数比较
    for (int i = 0; i < 5; i++) 
    {
        a.push(i);
        c.push(i);
    }
    while (!a.empty()) 
    {
        cout << a.top() << ' ';
        a.pop();
    } 
    cout << endl;
    while (!c.empty()) 
    {
        cout << c.top() << ' ';
        c.pop();
    }
    cout << endl;
	//字符串比较
    b.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty()) 
    {
        cout << b.top() << ' ';
        b.pop();
    } 
    cout << endl;
    //pair比较
    pair<int, int> b(1, 2);
    pair<int, int> c(1, 3);
    pair<int, int> d(2, 5);
    a.push(d);
    a.push(c);
    a.push(b);
    while (!a.empty()) 
    {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }
    return 0;
}

输出

4 3 2 1 0
0 1 2 3 4
cbd abcd abc
2 5
1 3
1 2

1.2 重载仿函数

如果一些基本类型,默认的仿函数无法满足我们的需求,那么我们可以重载仿函数

#include <iostream>
#include <queue>

using namespace std;

class mygreater{
    public:
        bool operator()(pair<int,int> t1,pair<int,int> t2)
        {
            int k1=t1.second/t1.first;
            int k2=t2.second/t2.first;
            return k1>k2;//小顶堆
        }
};


int main(){
    priority_queue<pair<int,int>,vector<pair<int,int>>,mygreater> p;
    p.push(pair<int,int>(2,4));
    p.push(pair<int,int>(2,2));
    p.push(pair<int,int>(1,6));
    p.push(pair<int,int>(3,9));
    while(!p.empty()){
        cout<<p.top().first<<" "<<p.top().second<<endl;
        p.pop();
    }
    return 0;
}

输出结果

2 2
2 4
3 9
1 6

注:同样运算符重载不能用于基本类型(int、pair),只能作用于结构体或类对象。

2、自定义类型

对于自定义类型,则必须自己重载 operator< 或者自己写仿函数operator()

方式一

定义类型,并重载operator<

#include <iostream>
#include <queue>

using namespace std;

class Node{
    public:
        int x,y;
        Node(int a=0,int b=0){
            x=a;
            y=b;
        }

};

bool operator<(Node t1,Node t2)
{
    int k1=t1.y/t1.x;
    int k2=t2.y/t2.x;
    return k1>k2;//小顶堆
}

int main(){
    priority_queue<Node> p;
    p.push(Node(1,4));
    p.push(Node(1,2));
    p.push(Node(1,3));
    while(!p.empty()){
        cout<<p.top().x<<" "<<p.top().y<<endl;
        p.pop();
    }
    return 0;
}

输出

1 2
1 3
1 4

自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。但此时不能像基本类型这样声明priority_queue<Node, vector<Node>, greater<Node>>;原因是 greater<Node> 没有定义,如果想用这种方法定义,则可以按如下方式二

方式二

定义类型,并自己写仿函数operator()

#include <iostream>
#include <queue>

using namespace std;

class Node{
    public:
        int x,y;
        Node(int a=0,int b=0){
            x=a;
            y=b;
        }

};
class mygreater{
    public:
        bool operator()(Node t1,Node t2)
        {
            int k1=t1.y/t1.x;
            int k2=t2.y/t2.x;
            return k1>k2;
        }
};


int main(){
    priority_queue<Node,vector<Node>,mygreater> p;
    p.push(Node(1,4));
    p.push(Node(1,2));
    p.push(Node(1,3));
    while(!p.empty()){
        cout<<p.top().x<<" "<<p.top().y<<endl;
        p.pop();
    }
    return 0;
}

输出

1 2
1 3
1 4

还有一点要注意的是priority_queue中的三个参数,后两个可以省去,因为有默认参数,不过如果,有第三个参数的话,必定要写第二个参数。

原文链接:https://blog.csdn.net/weixin_36888577/article/details/79937886

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值