【STL】使用priority_queue构造堆

使用stl queue中的priority_queue构造最大最小堆

#include 
#include 
#include 
using namespace std;

int main()
{
    // 默认构造最大堆
    priority_queue big_heap;
    // 构造最小堆
    priority_queue, greater> small_heap;
    // 构造最大堆
    priority_queue, less> big_heap2;
    if (big_heap.empty())
    {
        cout << "big heap is empty" << endl;
    }
    int test[] = {1, 5, 3, 2, 4, 9};
    for (int i = 0; i < 6; i++)
        big_heap.push(test[i]);
    while(!big_heap.empty()){
        cout << big_heap.top()<<" ";
        big_heap.pop();
    }
    return 0;
}

可以通过这道leetcode题练习一下
295. 数据流的中位数
使用priority_queue后就变成非常简单的一道题了

题解移步:
https://www.jianshu.com/p/41b97bed1f83

你可能感兴趣的:(【STL】使用priority_queue构造堆)