C++ STL 堆

#include <queue>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    //大根堆
    //push,pop,top,empty,size
    priority_queue<int> a;
    a.push(3);
    a.push(1);
    a.push(20);
    a.push(15);
    cout << a.top() << endl;
    //小根堆
    priority_queue<int, vector<int>, greater<int>> b;
    b.push(3);
    b.push(1);
    b.push(20);
    b.push(15);
    cout << b.top() << endl;
    //使用几个根堆相关的函数
    //make_heap: 根据指定的迭代器区间以及一个可选的比较函数,来创建一个heap. O(N)
    //push_heap: 把指定区间的最后一个元素插入到heap中. O(logN)
    //pop_heap: 弹出heap顶元素, 将其放置于区间末尾. O(logN)
    //sort_heap:堆排序算法,通常通过反复调用pop_heap来实现. N*O(logN)
    vector<int> c = {3, 1, 20, 15, 6, 8, 9, 2};
    make_heap(c.begin(), c.end());
    for(auto cc:c)
        cout << cc << " ";
    cout << endl;
    pop_heap(c.begin(), c.end());
    for(auto cc:c)
        cout << cc << " ";
    cout << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GarenJian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值