C++ heap

 make_heap();、pop_heap();、push_heap();、sort_heap();

他们的头函数是algorithm

首先是make_heap();

他的函数原型是:

void make_heap(first_pointer,end_pointer,compare_function);

一个参数是数组或向量的头指针,第二个向量是尾指针。第三个参数是比较函数的名字
。在缺省的时候,默认是大跟堆。(下面的参数都一样就不解释了)

作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)

然后是pop_heap();

它的函数原型是:

void pop_heap(first_pointer,end_pointer,compare_function);

作用:pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆。它
把first和last交换,然后将[first,last-1)的数据再做成一个堆。

接着是push_heap()

void pushheap(first_pointer,end_pointer,compare_function);

作用:push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加
进来,做成一个堆。

最后是sort_heap()

void sort_heap(first_pointer,end_pointer,compare_function);

作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然
,经过排序之后就不是一个有效堆了)

下面是例程:

#include<algorithm>

#include<cstdio>

using namespace std;

bool cmp(int a,int b)

{

return a>b;

}

int main()

{

int i,number[20]={29,23,20,22,17,15,26,51,19,12,35,40};

make_heap(&number[0],&number[12]);

//结果是:51 35 40 23 29 20 26 22 19 12 17 15

for(i=0;i<12;i++)

printf("%d ",number[i]);

printf("\n");

make_heap(&number[0],&number[12],cmp);

//结果:12 17 15 19 23 20 26 51 22 29 35 40

for(i=0;i<12;i++)

printf("%d ",number[i]);

printf("\n");

//加入元素8

number[12]=8;

//加入后调整

push_heap(&number[0],&number[13],cmp);

//结果:8 17 12 19 23 15 26 51 22 35 40 20

for(i=0;i<13;i++)

printf("%d ",number[i]);

printf("\n");

//弹出元素8

pop_heap(&number[0],&number[13],cmp);

//结果:12 17 15 19 23 20 26 51 22 29 35 40

for(i=0;i<13;i++)

printf("%d ",number[i]);

printf("\n");

sort_heap(&number[0],&number[12],cmp);

//结果不用说都知道是有序的了!

for(i=0;i<12;i++)

printf("%d ",number[i]);

return 0;

}

 

网上资料:

http://wenku.baidu.com/view/6adf1773f242336c1eb95ef2.html

http://www.docin.com/p-159439083.html

http://www.cnblogs.com/rocketfan/archive/2009/11/28/1612665.html

### C++ 堆的使用与管理 #### 使用智能指针提高堆内存的安全性和效率 为了更好地管理和控制动态分配的对象,C++ 提供了三种主要类型的智能指针:`std::unique_ptr`, `std::shared_ptr`, 和 `std::weak_ptr`。这些工具可以帮助开发者自动处理对象生命周期并防止常见的编程错误。 - **独占所有权**:当只需要单一拥有者时可以采用 `std::unique_ptr` 来确保资源不会被意外复制或共享[^1]。 ```cpp std::unique_ptr<int> ptr(new int(42)); ``` - **共享所有权**:对于多个部分可能需要访问同一对象的情况,则应考虑利用 `std::shared_ptr` 实现引用计数机制来跟踪有多少个实体正在持有该对象实例。 ```cpp auto sptr = std::make_shared<std::string>("hello"); ``` - **弱引用关系**:为了避免循环引用导致无法释放内存的问题,在某些情况下还可以借助于 `std::weak_ptr` 创建不增加引用次数但仍可获取目标对象的有效视图。 ```cpp std::weak_ptr<std::string> wptr(sptr); if (auto lockedPtr = wptr.lock()) { // Use *lockedPtr safely here. } ``` #### 分析程序中的堆行为 除了通过智能指针简化手动操作外,有时还需要深入了解应用程序运行期间实际发生的堆活动情况。为此目的而设计的一种方法就是启用堆分析器(Heap Profiler),它能够帮助定位潜在性能瓶颈以及发现未正确清理的数据结构[^2]。 要集成此功能通常涉及编译选项配置及特定库文件链接步骤;具体做法取决于所使用的开发环境和支持平台特性。一旦设置完成之后便可以在代码里调用相应接口函数触发采样过程: ```cpp #include <gperftools/heap-profiler.h> // Start profiling before critical section starts... HeapProfilerStart("my_program.prof"); // Critical code path goes here... // Stop profiler after finishing work on heap allocations. HeapProfilerStop(); ``` 上述片段展示了如何围绕待测逻辑包裹起始停止命令以便收集有用统计信息用于后续审查工作。 #### 防范悬挂指针风险 最后值得注意的是不当解除已销毁对象上的指针可能导致严重后果——即所谓“悬挂指针”。下面给出了一段存在此类隐患的例子说明为何应当谨慎对待指向动态创建变量的句柄[^3]。 ```cpp class A { int value; public: void dumb() const { cout << "dumb()\n"; } void set(int x) { cout << "set()\n"; value = x; } int get() const { cout << "get()\n"; return value; } }; int main() { A* pA1 = new A; A* pA2 = nullptr; try { pA1->dumb(); pA1->set(10); pA1->get(); delete pA1; // Object destroyed but pA1 still points to old location. // Attempting operations through dangling pointer leads to undefined behavior. pA1->dumb(); // Potential crash or other unpredictable outcomes. } catch (...) { cerr << "An exception occurred." << endl; } return 0; } ``` 这里尝试解引用已经被删除掉的目标地址将会引发不可预测的行为甚至崩溃。因此建议总是确认指针状态有效再执行任何依赖它的动作,并尽可能早地将其置为空值(`nullptr`)以减少误用可能性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值