zoukankan      html  css  js  c++  java
  • STL——heap的4大操作

    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)是一个有效的堆如果不是有效堆就老老实实用make_heap()

    然后,再把堆中的新元素加进来,做成一个堆。


    最后是sort_heap()void sort_heap(first_pointer,end_pointer,compare_function);

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

    下面是一个数组heap的例子

     1 #include<algorithm>
     2 #include<cstdio>
     3 using namespace std;
     4 
     5 bool cmp(int a,int b)//小根堆,降序排序 
     6 {
     7     return a>b;
     8 }
     9 
    10 int main()
    11 {
    12     int i,number[20]={29,23,20,22,17,15,26,51,19,12,35,40};
    13     
    14     make_heap(&number[0],&number[12]);
    15     //结果是:51 35 40 23 29 20 26 22 19 12 17 15
    16     for(i=0;i<12;i++)
    17     {
    18         printf("%d ",number[i]);
    19     }
    20     printf("
    ");
    21 
    22     make_heap(&number[0],&number[12],cmp);
    23     //结果:12 17 15 19 23 20 26 51 22 29 35 40
    24     for(i=0;i<12;i++)
    25     {
    26         printf("%d ",number[i]);
    27     }
    28     printf("
    ");
    29 
    30     //加入元素8
    31     number[12]=8;
    32     //加入后调整
    33     push_heap(&number[0],&number[13],cmp);
    34     //结果:8 17 12 19 29 15 26 22 23 51 35 40 20
    35     for(i=0;i<13;i++)
    36     {
    37         printf("%d ",number[i]);
    38     }
    39     printf("
    ");
    40 
    41     //弹出元素8
    42     pop_heap(&number[0],&number[13],cmp);
    43     //结果:12 17 15 19 23 20 26 51 22 29 35 40 8
    44     for(i=0;i<13;i++)
    45     {
    46         printf("%d ",number[i]);
    47     }
    48     printf("
    ");
    49     
    50     sort_heap(&number[0],&number[12],cmp);    //降序 
    51     //结果不用说都知道是有序的了!
    52     for(i=0;i<12;i++)
    53     {
    54         printf("%d ",number[i]);
    55     }
    56     return 0;
    57 }

    下面是一个vector heap的例子

      1 #include <iostream>
      2 #include <algorithm>
      3 #include <vector>
      4 using namespace std;
      5 
      6 const int VECTOR_SIZE = 8 ;
      7 
      8 int main()
      9 {
     10     vector<int, allocator<int> >Numbers(VECTOR_SIZE) ;
     11     vector<int, allocator<int> >::iterator it ;
     12 
     13     // Initialize vector Numbers
     14     Numbers[0] = 4 ;
     15     Numbers[1] = 10;
     16     Numbers[2] = 70 ;
     17     Numbers[3] = 10 ;
     18     Numbers[4] = 30 ;
     19     Numbers[5] = 69 ;
     20     Numbers[6] = 96 ;
     21     Numbers[7] = 100;
     22 
     23     // print content of Numbers
     24     cout << "Numbers { " ;
     25     for(it = Numbers.begin(); it != Numbers.end(); it++)
     26     {
     27         cout << *it << " " ;
     28     }
     29     cout << " }
    " << endl ;
     30 
     31 
     32     // convert Numbers into a heap
     33     make_heap(Numbers.begin(), Numbers.end()) ;    //缺省参数生成大根堆 
     34     
     35     cout << "After calling make_heap
    " << endl ;
     36     // print content of Numbers
     37     cout << "Numbers { " ;
     38     for(it = Numbers.begin(); it != Numbers.end(); it++)
     39     { 
     40         cout << *it << " " ;
     41     } 
     42     cout << " }
    " << endl ;
     43 
     44 
     45     // sort the heapified sequence Numbers
     46     sort_heap(Numbers.begin(), Numbers.end()) ;    //缺省参数默认升序排列 
     47     
     48     cout << "After calling sort_heap
    " << endl ;
     49     // print content of Numbers
     50     cout << "Numbers { " ;
     51     for(it = Numbers.begin(); it != Numbers.end(); it++)
     52     { 
     53         cout << *it << " " ;
     54     } 
     55     cout << " }
    " << endl ;
     56     
     57     
     58     //insert an element in the heap
     59     Numbers.push_back(7) ;                                    //在堆的尾部添加一个数据 
     60     //push_heap(Numbers.begin(), Numbers.end()) ;    //因为之前使用sort对堆进行了排序,所以 
     61                                                                     //[first,last-1)不是一个有效的堆,push_heap没效果 
     62     // you need to call make_heap to re-assert the
     63     // heap property
     64     make_heap(Numbers.begin(), Numbers.end()) ;        //使用这个直接全部重新生成新堆 
     65     
     66     cout << "After calling push_heap and make_heap
    " << endl ;
     67     
     68     // print content of Numbers
     69     cout << "Numbers { " ;
     70     for(it = Numbers.begin(); it != Numbers.end(); it++)
     71     { 
     72         cout << *it << " " ;
     73     }
     74     cout << " }
    " << endl ;
     75     
     76     // remove the root element from the heap Numbers
     77     pop_heap(Numbers.begin(), Numbers.end()) ;
     78     
     79     cout << "After calling pop_heap
    " << endl ;
     80     
     81     // print content of Numbers
     82     cout << "Numbers { " ;
     83     for(it = Numbers.begin(); it != Numbers.end(); it++)
     84     {
     85         cout << *it << " " ;
     86     }
     87     cout << " }
    " << endl ;
     88     return 0; 
     89 }
     90 /*
     91 程序输出为:
     92 
     93 Numbers { 4 10 70 10 30 69 96 100 }
     94 之后调用 make_heap
     95 
     96 Numbers { 100 30 96 10 4 69 70 10 }
     97 之后调用 sort_heap
     98 
     99 Numbers { 4 10 10 30 69 70 96 100 }
    100 之后调用 push_heap 和 make_heap
    101 
    102 Numbers { 100 69 96 30 4 70 10 10 7 }
    103 之后调用 pop_heap
    104 
    105 Numbers { 96 69 70 30 4 7 10 10 100 }
    106 */
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    live 555 freebsd 或centos 7.4 实现代理视频直播服务
    [教学] Log.d 日志调试查看(所有平台)
    [示例] 使用 TStopwatch 计时
    [安裝] Ubuntu Server 初试
    [改善] dcef3 for Firemonkey 浏览器控件,拉动窗体大小会闪烁的问题
    [示例] Drag And Drop for FireMonkey (Win & macOS)
    [原创] 改善 Firemonkey Canvas 几何绘图质量问题(移动平台)
    [示例] Firemonkey 画出 1 点像素的线
    [修正] Firemonkey TFrame 存档后,下次载入某些事件连结会消失(但源码还在)
    [问答] Firemonkey 控件继承后无法显示(空白)
  • 原文地址:https://www.cnblogs.com/pingge/p/3200383.html
Copyright © 2011-2022 走看看