STL源码剖析笔记(heap)

函数make_heap的使用

make_heap(iterator first,iterator last) 可生成一个最大堆

push_heap(iterator first,iterator last) 前提是[first,last-1)是一个堆,改函数可将last-1元素放入堆中,维护堆的特性

pop_heap(iterator first,iterator last)  取出堆中的最大值,放入last-1中,维护[first,last-1) 的堆的特性

sort_heap(iterator first,iterator last) 对堆从小到大排序(内部是不断push_heap,直至元素清空)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int p[100];
    for(int i=0;i<10;i++)p[i]=i;

    make_heap(p,p+10);
    for(int i=0;i<4;i++){
        cout<<p[i]<<" "<<p[i*2+1]<<" "<<p[i*2+2]<<"\n";
    }
    return 0;
}
output:
9 8 6
8 7 4
6 5 2
7 0 3

猜你喜欢

转载自blog.csdn.net/jfnfjivkdnfjf/article/details/81241187