头文件queue主要包括循环队列queue和优先队列priority_queue两个容器。
声明:
#include <iostream>#include <queue>#include <vector>using namespace std;struct air {int x, y;bool operator< (const air& t) const {return y < t.y;}};int main() {queue<int> a;queue<air> b;priority_queue<int> c; // 默认为大根堆priority_queue<air> e; // 大根堆,得重载<priority_queue<air, vector<air>, greater<air>> d; // 小根堆得重载>e.push({1, 2});e.push({2, 1});e.push({3, 0});cout << e.top().x << " " << e.top().y << endl;e.pop();cout << e.top().x << " " << e.top().y << endl;return 0;}
使用优先队列时,默认是大根堆,自定义结构体或类必须重载<
如果使用小根堆形式,自定义结构体或类必须重载>
push() |
从队尾插入 | |
|---|---|---|
pop() |
弹出队头元素 | |
top() |
返回队头元素 |
:::danger
队列,优先队列,栈没有clear()
:::
