优先级队列分为最大值优先队列、最小值优先队列
优先级队列适配器:STL priority_queue
优先级队列一般用来开发特殊应用
代码:
#include<iostream> using namespace std; #include<queue> void main81() { priority_queue<int> p1;//默认情况是最大优先级队列 priority_queue<int, vector<int>, less<int>> p2;//less是提前定义好的函数 相当于谓词 priority_queue<int, vector<int>, greater<int>> p3;//最小值有限队列 p1.push(33); p1.push(13); p1.push(55); p1.push(22); cout << "队头元素:" << p1.top() << endl; cout << "队列大小: " << p1.size() << endl; while (p1.size()) { cout << p1.top() << " "; p1.pop(); } cout << "测试 最小值优先级队列" << endl; p3.push(33); p3.push(13); p3.push(55); p3.push(22); cout << "最小值优先级队列 队头元素:" << p3.top() << endl; cout << "最小值优先级队列 队列大小: " << p3.size() << endl; while (p3.size()) { cout << p3.top() << " "; p3.pop(); } } int main() { main81(); system("pause"); return 0; }