一个FLAG #16# 优先队列
例子
#include <iostream> #include <queue> #include <vector> using namespace std; struct cmp { bool operator() (const int a, const int b) const { return a % 10 < b % 10; } }; int main() { // 默认的优先队列。数值越大优先级越高 priority_queue<int> pq; pq.push(2); pq.push(123); pq.push(1235); pq.push(55); pq.push(1230); cout << pq.top() << endl; pq.pop(); // => 1235 cout << pq.top() << endl; pq.pop(); // => 1230 cout << pq.top() << endl; pq.pop(); // => 123 cout << pq.top() << endl; pq.pop(); // => 55 cout << pq.top() << endl; pq.pop(); // => 2 cout << pq.top() << endl; pq.pop(); // => 2 cout << pq.top() << endl; pq.pop(); // => 2 cout << pq.top() << endl; pq.pop(); // => 2 cout << " ==================================== == =" << endl; // 越小的整数优先级越高 priority_queue<int, vector<int>, greater<int> > pq2; pq2.push(2); pq2.push(123); pq2.push(55); cout << pq2.top() << endl; pq2.pop(); // => 2 cout << pq2.top() << endl; pq2.pop(); // => 55 cout << pq2.top() << endl; pq2.pop(); // => 123 cout << " ==================================== == =" << endl; // 自定义的优先队列,个位越大的优先级越高 priority_queue<int, vector<int>, cmp> pq3; pq3.push(2); pq3.push(123); pq3.push(55); cout << pq3.top() << endl; pq3.pop(); // => 55 cout << pq3.top() << endl; pq3.pop(); // => 123 cout << pq3.top() << endl; pq3.pop(); // => 2 return 0; }