zoukankan      html  css  js  c++  java
  • STL之heap

    STL的堆操作

    STL里面的堆操作一般用到的只有4个:make_heap();、pop_heap();、push_heap();、sort_heap();

    他们的头文件函数是#include <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]);



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

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

    printf(" ");

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



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

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

    printf(" ");

    //加入元素8

    number[12]=8;

    //加入后调整

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



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

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

    printf(" ");

    //弹出元素8

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


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

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

    printf(" ");

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

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

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

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

    return 0;

    }

    1. #include <cstdio>  
    2. #include <vector>  
    3. #include <algorithm>  
    4. #include <functional>  
    5. using namespace std;  
    6. void PrintfVectorInt(vector<int> &vet)  
    7. {  
    8.     for (vector<int>::iterator pos = vet.begin(); pos != vet.end(); pos++)  
    9.         printf("%d ", *pos);  
    10.     putchar(' ');  
    11. }  
    12. int main()  
    13. {  
    14.     const int MAXN = 20;  
    15.     int a[MAXN];  
    16.     int i;  
    17.     for (i = 0; i < MAXN; ++i)  
    18.         a[i] = rand() % (MAXN * 2);  
    19.   
    20.     //动态申请vector 并对vector建堆  
    21.     vector<int> *pvet = new vector<int>(40);  
    22.     pvet->assign(a, a + MAXN);  
    23.   
    24.     //建堆  
    25.     make_heap(pvet->begin(), pvet->end());  
    26.     PrintfVectorInt(*pvet);  
    27.   
    28.     //加入新数据 先在容器中加入,再调用push_heap()  
    29.     pvet->push_back(25);  
    30.     push_heap(pvet->begin(), pvet->end());  
    31.     PrintfVectorInt(*pvet);  
    32.   
    33.     //删除数据  要先调用pop_heap(),再在容器中删除  
    34.     pop_heap(pvet->begin(), pvet->end());  
    35.     pvet->pop_back();  
    36.     pop_heap(pvet->begin(), pvet->end());  
    37.     pvet->pop_back();  
    38.     PrintfVectorInt(*pvet);  
    39.   
    40.     //堆排序  
    41.     sort_heap(pvet->begin(), pvet->end());  
    42.     PrintfVectorInt(*pvet);  
    43.   
    44.     delete pvet;  
    45.     return 0;
    46. }   
    47.   
  • 相关阅读:
    Linux定时任务编写
    Linux编辑器的选择使用
    nginx配置中文域名解析
    Linux中统计某个文件夹的大小
    nginx配置文件的说明
    获取自身ip
    Python之时间模块、random模块、json与pickle模块
    python之os模块详解
    日志模块logging介绍
    Python面向过程、模块的使用
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/3753628.html
Copyright © 2011-2022 走看看