优先队列和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队。优先队列具有队列的所有特性,包括基本操作,只是在这基础上添加了内部的一个排序,它本质是一个堆实现的。
和队列基本操作相同::
- top 访问队头元素
- empty 队列是否为空
- size 返回队列内元素个数
- push 插入元素到队尾 (并排序)
- emplace 原地构造一个元素并插入队列
- pop 弹出队头元素
- swap 交换内容
主要内容包括:
- 大顶堆,降序排列
- 小顶堆,升序排列
- pair的比较
- 自定义类型
- 自定义键值对类型
1. 大顶堆,降序排列
#include<iostream>
#include <queue>
using namespace std;
int main()
{
//对于基础类型 默认是大顶堆
priority_queue<int> a;
//等同于 priority_queue<int, vector<int>, less<int> > a;
// 小的往后排
priority_queue<string> b;
for (int i = 0; i < 5; i++)
{
a.push(i);
}
while (!a.empty())
{
cout << a.top() << ' ';
a.pop();
}
cout << endl;
b.push("ac");
b.push("abc");
b.push("abcd");
b.push("cbd");
while (!b.empty())
{
cout << b.top() << ' ';
b.pop();
}
cout << endl;
return 0;
}
// 输出:
// 4 3 2 1 0
// cbd ac abcd abc
2. 小顶堆,升序排列
#include<iostream>
#include <queue>
using namespace std;
int main()
{
//小顶堆, 升序排列
priority_queue<int, vector<int>, greater<int>> a;
priority_queue<string, vector<string>, greater<string>> b;
// 大的往后排
for (int i = 0; i < 5; i++)
{
a.push(i);
}
while (!a.empty())
{
cout << a.top() << ' ';
a.pop();
}
cout << endl;
b.push("ac");
b.push("abc");
b.push("abcd");
b.push("cbd");
while (!b.empty())
{
cout << b.top() << ' ';
b.pop();
}
cout << endl;
return 0;
}
// 输出
// 0 1 2 3 4
// abc abcd ac cbd
3. pair的比较
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
// 小顶堆,升序排列
// priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> a;
// 大顶堆,将序排列
priority_queue<pair<int, int> > a;
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();
}
}
// 大顶堆输出
// 2 5
// 1 3
// 1 2
4. 自定义类型
写法1
//方法1,
struct tmp1 //运算符重载<
{
int x;
tmp1(int a) { x = a; }
// 必须得实现比较运算符
bool operator<(const tmp1& a) const
{
return x < a.x; //大顶堆
// 小的往后排
}
};
int main()
{
tmp1 a(1);
tmp1 b(2);
tmp1 c(3);
priority_queue<tmp1> d;
d.push(b);
d.push(c);
d.push(a);
while (!d.empty())
{
cout << d.top().x << ' ';
d.pop();
}
cout << endl;
// 3 2 1
}
写法2 重写仿函数
//方法1
struct tmp1 //运算符重载<
{
int x;
tmp1(int a) { x = a; }
bool operator<(const tmp1& a) const
{
return x < a.x; //大顶堆
}
};
//方法2
struct tmp2 //重写仿函数
{
bool operator() (tmp1 a, tmp1 b)
{
return a.x > b.x; //小顶堆
// 大的往后排
}
};
int main()
{
tmp1 a(1);
tmp1 b(2);
tmp1 c(3);
priority_queue<tmp1, vector<tmp1>, tmp2> f;
// 这种写法排序,看仿函数如何排序
// 自身的重写的运算符不起作用
// 甚至可以删除自身的运算符
f.push(c);
f.push(b);
f.push(a);
while (!f.empty())
{
cout << f.top().x << ' ';
f.pop();
}
}
// 1 2 3
写法3 重写仿函数,删除重写的比较运算符
//方法1
struct tmp1 //运算符重载<
{
int x;
tmp1(int a) { x = a; }
// bool operator<(const tmp1& a) const
// {
// return x < a.x; //大顶堆
// }
};
//方法2
struct tmp2 //重写仿函数
{
bool operator() (tmp1 a, tmp1 b)
{
return a.x > b.x; //小顶堆
// 大的往后排
}
};
int main()
{
tmp1 a(1);
tmp1 b(2);
tmp1 c(3);
priority_queue<tmp1, vector<tmp1>, tmp2> f;
// 这种写法排序,看仿函数如何排序
// 自身的重写的运算符不起作用
// 甚至可以删除自身的运算符
f.push(c);
f.push(b);
f.push(a);
while (!f.empty())
{
cout << f.top().x << ' ';
f.pop();
}
}
// 1 2 3
5. 自定义键值对类型
写法1,仿函数 小根堆
#include<iostream>
#include<queue>
using namespace std;
struct Node {
int x, y;
Node(int a = 0, int b = 0) :
x(a), y(b) {}
};
struct cmp {
bool operator()(Node a, Node b) {
if (a.x == b.x) return a.y > b.y;
return a.x > b.x; // 小根堆,大的往后排
}
};
int main() {
priority_queue<Node, vector<Node>, cmp>p;
p.push(Node(3, 2));
p.push(Node(1, 2));
p.push(Node(1, 3));
p.push(Node(2, 3));
p.push(Node(3, 3));
while (!p.empty()) {
cout << p.top().x << ' ' << p.top().y << endl;
p.pop();
}
return 0;
}
// 输出
// 1 2
// 1 3
// 2 3
// 3 2
// 3 3
写法2 大根堆
#include<iostream>
#include<queue>
using namespace std;
struct Node {
int x, y;
Node(int a = 0, int b = 0) :
x(a), y(b) {}
bool operator<(const Node& b)const {
if (x == b.x) return y < b.y;
return x < b.x; // 大根堆,小的往后排
}
};
int main() {
priority_queue<Node>p;
p.push(Node(3, 2));
p.push(Node(1, 2));
p.push(Node(1, 3));
p.push(Node(2, 3));
p.push(Node(3, 3));
while (!p.empty()) {
cout << p.top().x << ' ' << p.top().y << endl;
p.pop();
}
return 0;
}
// 输出
//3 3
//3 2
//2 3
//1 3
//1 2