STL_make_heap(), pop_heap(), push_heap()

本文详细介绍了如何使用make_heap、push_heap和pop_heap等算法操作大顶堆和小顶堆。通过具体的代码示例展示了如何创建堆、插入元素以及弹出堆顶元素的过程。

make_heap()是生成一个堆,大顶堆或小顶堆
make_heap(_RAIter,_RAIter) 默认生成大顶堆
make_heap(_RAIter,_RAIter,_Compare) _Compare有两种参数,一种是greater(生成小顶堆),一种是less(生成大顶堆)


push_heap()是向堆中插入一个元素,并且使堆的规则依然成立
push_heap(_RAIter,_RAIter) 默认为大顶堆
push_heap(_RAIter,_RAIter,_Compare) _Compare有两种参数,一种是greater(小顶堆),一种是less(大顶堆)
调用push_heap之前必须调用make_heap创建一个堆
首先数组push_back插入元素,然后再调用push_heap,它会使最后一个元素插到合适位置
注意,push_heap中的_Compare和make_heap中的_Compare参数必须是一致的,不然会插入堆失败,最后一个元素还是在最后位置,导致插入失败


pop_heap()是在堆的基础上,弹出堆顶元素。
pop_heap(_RAIter,_RAIter) 默认为大顶堆
pop_heap(_RAIter,_RAIter,_Compare) _Compare有两种参数,一种是greater(小顶堆),一种是less(大顶堆)
比如pop_heap(nums.begin(), nums.end(),greater<int>()),它会将堆顶元素(即为数组第一个位置)和数组最后一个位置对调,然后你可以调用数组pop_back,删除这个元素
注意,pop_heap中的_Compare和make_heap中的_Compare参数必须是一致的,不然会失败
 

# include <iostream>
# include <functional>
# include <vector>
# include <algorithm>

using namespace std;

void printVec(vector<int> nums)
{
    for (int i = 0; i < nums.size(); ++i)
        cout << nums[i] << " ";
    cout << endl;
}
int main(void)
{
    int nums_temp[] = {8, 3, 4, 8, 9, 2, 3, 4, 10};
    vector<int> nums(nums_temp, nums_temp + 9);
    cout << "make_heap之前: ";
    printVec(nums);

    cout << "(默认(less))make_heap: ";
    make_heap(nums.begin(), nums.end());
    printVec(nums);

    cout << "(less)make_heap: ";
    make_heap(nums.begin(), nums.end(), less<int> ());
    printVec(nums);

    cout << "(greater)make_heap: ";
    make_heap(nums.begin(), nums.end(), greater<int> ());
    printVec(nums);

    cout << "此时,nums为小顶堆 greater" << endl;
    cout << "push_back(3)" << endl;
    nums.push_back(3);
    cout << "默认(less)push_heap 此时push_heap失败: ";
    push_heap(nums.begin(), nums.end());
    printVec(nums);
    cout << "push_heap为greater 和make_heap一致,此时push_heap成功: ";
    push_heap(nums.begin(), nums.end(), greater<int>());
    printVec(nums);
    cout << "(greater,不然会失败)pop_heap: ";
    pop_heap(nums.begin(), nums.end(),greater<int>());
    printVec(nums);
    cout << "pop_back(): ";
    nums.pop_back();
    printVec(nums);
}

转自:https://blog.youkuaiyun.com/qq_29630271/article/details/66478256

### C++ 堆维护的实现方式 在 C++ 中,堆的维护可以通过 `make_heap`、`push_heap` 和 `pop_heap` 等标准库函数来实现。这些函数属于 STL 提供的堆算法,允许开发者在不手动管理堆结构的情况下,高效地操作堆数据[^4]。 #### `make_heap` `make_heap` 函数用于将一个范围内的元素构造成堆结构。它接受两个随机访问迭代器作为参数,表示需要构造堆的范围,并可以选择提供一个比较函数以定义堆的排序规则。默认情况下,`make_heap` 构造的是一个最大堆[^2]。 例如,可以使用 `make_heap` 构造一个最大堆: ```cpp #include <vector> #include <algorithm> #include <iostream> int main() { std::vector<int> data = {5, 3, 8, 1, 2}; std::make_heap(data.begin(), data.end()); std::cout << "堆顶元素: " << data.front() << std::endl; // 输出最大值 return 0; } ``` #### `push_heap` 当需要向堆中插入新元素时,首先需要将元素添加到容器末尾,然后调用 `push_heap` 来恢复堆的性质。该函数假设新元素已经被放置在容器的最后一个位置,并对前 `n` 个元素重新构造堆结构[^1]。 示例代码如下: ```cpp #include <vector> #include <algorithm> #include <iostream> int main() { std::vector<int> data = {5, 3, 8, 1, 2}; std::make_heap(data.begin(), data.end()); data.push_back(10); // 插入新元素 std::push_heap(data.begin(), data.end()); // 重新构造堆 std::cout << "插入10后的堆顶: " << data.front() << std::endl; return 0; } ``` #### `pop_heap` `pop_heap` 用于将堆顶元素(最大值)移动到容器末尾,然后对前 `n-1` 个元素重新构造堆结构。需要注意的是,调用 `pop_heap` 后,堆的大小减少一个元素,但容器的大小不变,堆顶元素被移动到容器的末尾,此时可以通过 `pop_back()` 删除它[^5]。 例如: ```cpp #include <vector> #include <algorithm> #include <iostream> int main() { std::vector<int> data = {5, 3, 8, 1, 2}; std::make_heap(data.begin(), data.end()); std::pop_heap(data.begin(), data.end()); // 将堆顶移动到末尾 int max = data.back(); // 获取堆顶元素 data.pop_back(); // 删除堆顶 std::cout << "移除堆顶后的堆顶元素: " << data.front() << std::endl; return 0; } ``` #### 堆维护的完整流程 在实际使用中,通常的流程是: 1. 使用 `make_heap` 初始化堆。 2. 使用 `push_heap` 添加新元素。 3. 使用 `pop_heap` 移除堆顶元素。 4. 若需要排序,使用 `sort_heap` 对堆进行排序。 例如,以下代码展示了如何在堆的基础上进行排序: ```cpp #include <vector> #include <algorithm> #include <iostream> int main() { std::vector<int> data = {5, 3, 8, 1, 2}; std::make_heap(data.begin(), data.end()); std::sort_heap(data.begin(), data.end()); // 对堆排序 for (int num : data) { std::cout << num << " "; } std::cout << std::endl; return 0; } ``` #### 时间复杂度分析 - `make_heap` 的时间复杂度为 O(n),其中 n 是容器中元素的数量。 - `push_heap` 和 `pop_heap` 的时间复杂度均为 O(log n),因为它们只涉及堆的高度。 - `sort_heap` 的时间复杂度为 O(n log n),与快速排序相当[^3]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值