定义: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