make_heap、pop_heap、push_heap用法

C++ STL中make_heap、pop_heap、push_heap操作详解
本文介绍了C++ STL中用于堆操作的`make_heap`、`pop_heap`和`push_heap`函数。`make_heap`能在容器中建立堆,保证最大值位于最前面;`pop_heap`将堆顶元素移至末尾并保持堆的性质,需额外取出最后一个元素;`push_heap`则用于在已有堆的容器中将新插入元素调整为堆结构,保持堆的正确性。这三个函数的时间复杂度均为O(logN)。
#include<iostream>
#include <functional>
using namespace std;
int main(){

  vector<int> arr = {1,2,2,3,3,4,6};
//less<int> ,greater<int> 要添加头文件#include <functional>
  make_heap(arr.begin(),arr.end(),less<int>())//大顶堆,默认情况下


  vector<int> arrr2 = {1,2,2,3,3,4,6};
  
  make_heap(arr2.begin(),arr2.end(),greater<int>())//小顶堆

  pop_heap(arr2.begin(0,arr2.end(),greater<int>())//建堆的第三个参数要一致即greater<int>
  arr2.pop_back();

  
  arr.push_back(5);
  push_heap(arr.begin(),arr.end(),less<int>());


  return 0;

}

less<int>()  大顶堆

greater<int>() 小顶堆 

make_heap (): 时间复杂度为O(N)
在容器范围内,就地建堆,保证最大值在所给范围的最前面,其他值的位置不确定

 

pop_heap () 时间复杂度为O(logN)
将堆顶(所给范围的最前面)元素移动到所给范围的最后,并且将新的最大值置于所给范围的最前面;

template <class RandomAccessIterator, class Compare>
  void pop_heap (RandomAccessIterator first, RandomA

### 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]。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值