priority_queue优先队列
A priority queue maintains a set of elements. The supported operations are insertion and, depending on the type of the queue, retrieval and removal of either the minimum or maximum element. Insertion and removal take time, and retrieval takes time.
priority_queue<int> q;
q.push(3);
q.push(5);
q.push(7);
q.push(2);
cout << q.top() << "\n"; // 7
q.pop();
cout << q.top() << "\n"; // 5
q.pop();
q.push(6);
cout << q.top() << "\n"; // 6
q.pop();
默认是大根堆,如果要写小根堆,就是push x
进去的时候,push成-x
,取出使用的时候,加个负号再使用。实现大根堆,还有一个方法,如下:
priority_queue<int,vector<int>,greater<int> >q;//这样就可以实现小根堆了
参考:https://www.cnblogs.com/zwfymqz/p/7800654.html, 如何实现自定义结构体的排序