昨天读了大佬的博客,今天自己测试敲一敲
主要收获在于理解了priority_queue的出队方式,
以前没有认真调试查看过,
惭愧啊.
以下是对f_zyj大佬模板中提供的priority_queue相关知识的测试和注解
f_zyj大佬的博客:
http://blog.csdn.net/f_zyj/article/details/51594851
地址为pdf中第16页
#define pause system("pause");
#define fence puts("-------------");
#define endline puts("")
#include <iostream>
#include <queue>
#include <functional>
using namespace std;
//定义priority_queue
//构造时有三个参数,后两个是默认参数,默认为vector<T>和less<int>
//分别是 type,allocate,算子
priority_queue<int>q;
priority_queue<pair<int, int>>qq;
priority_queue<int, vector<int>, greater<int>>qqq;
//定义算子类
class T
{
public:
int x, y, z;
T(int a, int b, int c) :x(a), y(b), z(c) {};
};
/*
priority_queue和queue有所不同
priority_queue出队时列尾元素先出列
而queue是列首元素先出列
了解了这一点
算子类就容易理解了
(这个之前困惑我好久,还是基础不扎实,感谢大佬解惑)
*/
//对于默认的less算子,调用下面这个函数确定优先级
bool operator < (const T &a, const T &b){
return a.z < b.z;
}
//qt内元素以z为关键字,由小到大,从队首到队尾,依次排列
priority_queue<T>qt;
/*
priority_queue的基本操作与stack类似,由以下操作构成
empty() 判空
size() 返回元素个数
pop() 删除队首元素
top() 返回优先级最高的元素,队尾元素
push() 根据优先级自动在合适的位置添加元素
*/
int main()
{
qt.push(T(1, 2, 3));
qt.push(T(2, 1, 1));
qt.push(T(3, 2, 2));
while (!qt.empty())
{
T t = qt.top();
qt.pop();
printf("%d %d %d\n", t.x, t.y, t.z);
}
pause
}
/*
1 2 3
3 2 2
2 1 1
*/