C++ —— 利用数组 vector 使用堆 heap

1. 头文件

堆的使用在头文件<algorithm>中定义

2. 创建堆:make_heap()

        默认创建大顶堆,如果想要创建小顶堆,则需要在函数添加第三个参数:greater<int>。

#include<iostream>
#include<algorithm>

using namespace std;

int main () {
    vector<int> heap = { 1,2,3,4,5,6,7,8,9,10 };
    cout << "原数组:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    make_heap (heap.begin () , heap.end ());
    cout << "建堆后:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    system ("pause");
    return 0;
}

3. 判断数组是否是堆 is_heap()

#include<iostream>
#include<algorithm>

using namespace std;

int main () {
    vector<int> heap = { 1,2,3,4,5,6,7,8,9,10 };
    make_heap (heap.begin () , heap.end ());
    cout << "原堆:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    cout << (is_heap (heap.begin () , heap.end ()) ? "Yes" : "No") << endl;
    //当我们再 push 一个数到数组中,将不再是一个堆
    heap.push_back (11);
    cout << (is_heap (heap.begin () , heap.end ()) ? "Yes" : "No") << endl;

    system ("pause");
    return 0;
}

4. 添加与删除 push_heap() & pop_heap()

4.1 push_heap()

        因为make_heap只能建堆,如果当前堆数据发生改变,就需要使用push_heap重回大堆/小堆。

        ps:first 到 last - 1 之间的元素必须满足堆结构。它仅仅是将数组中的最后一个元素加入到堆中。

#include<iostream>
#include<algorithm>

using namespace std;

int main () {
    vector<int> heap = { 1,2,3,4,5,6,7,8,9,10 };
    make_heap (heap.begin () , heap.end ());
    cout << "原堆:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    heap.push_back (11);
    push_heap (heap.begin () , heap.end ());
    cout << "插入元素到堆中:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    system ("pause");
    return 0;
}

4.2 pop_heap()

       类似于 push_heap ,pop_heap 也仅仅是将堆顶元素放入到数组的尾部

#include<iostream>
#include<algorithm>

using namespace std;

int main () {
    vector<int> heap = { 1,2,3,4,5,6,7,8,9,10 };
    make_heap (heap.begin () , heap.end ());
    cout << "原堆:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    pop_heap (heap.begin () , heap.end ());
    cout << "删除堆顶元素后:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    system ("pause");
    return 0;
}

5. 堆排序sort_heap()

        默认按升序将堆排序,如果想要按降序排序,则需要在函数添加第三个参数:greater<int>。

#include<iostream>
#include<algorithm>

using namespace std;

int main () {
    vector<int> heap = { 1,2,3,4,5,6,7,8,9,10 };
    make_heap (heap.begin () , heap.end ());
    cout << "原堆:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    sort_heap (heap.begin () , heap.end ());
    cout << "排序后:";
    for (int x : heap) {
        cout << x << " ";
    }
    cout << endl;

    system ("pause");
    return 0;
}

6. 另外的使用方法

        在 C++ 的另一个头文件<queue>中,有一个叫做 priority_queue 的容器,如果熟悉堆(或者说优先队列)的话,你或许已经知道如何使用了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值