队列
队列先进先出
队列不可以用q[2]来访问,只能用迭代器和top()
优先队列:priority_queue q;
操作函数
push() o(logn)
top() 优先输出最大的数
基础知识
大根堆
排序
默认
priority_queue q;//默认是从大到小
priority_queue<int, vector ,less >q;//从大到小排序
priority_queue<int, vector, greater >q;//从小到大排序
自定义1:return 1就是小的意思
weight大的优先级高
struct cmp1
{
bool operator () (const Node &a, const Node &b)
{
return a.weight < b.weight;
}
};
priority_queue< Node, vector<Node>, cmp1> q1;
weight小的优先级高
struct cmp2
{
bool operator()(const Node &a, const Node &b)
{
return a.weight > b.weight;
}
};
priority_queue<Node,vector<Node>,cmp2> q2;
自定义2:return 1就是小的意思
weight大的先
struct Node2
{
char data;
int weight;
Node2(char _data, int _weight)
{
data = _data;
weight = _weight;
}
friend bool operator < (const Node2 &a,const Node2 &b)
{
return a.weight < b.weight;
}
};
priority_queue<Node2> q3;
双端队列
deque双端队列(效率较低)