STL里面的堆操作

STL里面的堆操作

STL里面的“堆操作” 一般用到的只有4个:

1、make_heap();                                                                   
2、pop_heap();
3、push_heap();
4、sort_heap();
他们的头文件函数是:#include <algorithm>
一、make_heap();

1、函数原型:void make_heap(first_pointer , end_pointer ,compare_function);
       第一个参数(first_pointer)是数组或向量的头指针,第二个(end_pointer)是尾指针,第三个(compare_function)是比较函数。在缺省的时候,默认是大根堆。(下面的参数都一样就不解释了)
2、作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)
二、pop_heap();

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

1、函数原型:void pushheap(first_pointer , end_pointer , compare_function);
2、作用:push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加进来,做成一个堆。
四、sort_heap()

1、函数原型:void sort_heap(first_pointer , end_pointer , compare_function);
2、作用:sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然,经过排序之后就不是一个有效堆了)
五、样例:

 
#include<algorithm>
#include<cstdio>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
inti,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;
}
Compile options needed: /GX
//
// heap_functions.cpp : Illustrates how to use the
// make_heap, sort_heap, push_heap
// and pop_heap functions.
//
// Functions:
//
// make_heap : convert a sequence to a heap
// sort_heap : sort a heap
// push_heap : insert an element in a heap
// pop_heap : remove the top element from a heap
//
// Written by Kalindi Sanghrajka
// of Microsoft Product Support Services,
// Software Core Developer Support.
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//
 
// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable: 4786)
 
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
void main()
{
const intVECTOR_SIZE = 8 ;
// Define atemplate class vector of int
typedefvector<int, allocator<int> > IntVector ;
//Define aniterator for template class vector of strings
typedefIntVector::iterator IntVectorIt ;
IntVectorNumbers(VECTOR_SIZE) ;
IntVectorIt it ;
// Initializevector Numbers
Numbers[0] = 4 ;
Numbers[1] = 10;
Numbers[2] = 70 ;
Numbers[3] = 10 ;
Numbers[4] = 30 ;
Numbers[5] = 69 ;
Numbers[6] = 96 ;
Numbers[7] = 100;
// print content ofNumbers
cout <<"Numbers { " ;
for(it =Numbers.begin(); it != Numbers.end(); it++)
cout << *it<< " " ;
cout <<" }\n" << endl ;
// convert Numbersinto a heap
make_heap(Numbers.begin(),Numbers.end()) ;
cout <<"After calling make_heap\n" << endl ;
// print content ofNumbers
cout <<"Numbers { " ;
for(it =Numbers.begin(); it != Numbers.end(); it++)
cout << *it<< " " ;
cout <<" }\n" << endl ;
// sort theheapified sequence Numbers
sort_heap(Numbers.begin(),Numbers.end()) ;
cout <<"After calling sort_heap\n" << endl ;
// print content ofNumbers
cout <<"Numbers { " ;
for(it =Numbers.begin(); it != Numbers.end(); it++)
cout << *it<< " " ;
cout <<" }\n" << endl ;
//insert an elementin the heap
Numbers.push_back(7);
push_heap(Numbers.begin(),Numbers.end()) ;
// you need to callmake_heap to re-assert the
// heap property
make_heap(Numbers.begin(),Numbers.end()) ;
cout <<"After calling push_heap and make_heap\n" << endl ;
// print content ofNumbers
cout <<"Numbers { " ;
for(it = Numbers.begin();it != Numbers.end(); it++)
cout << *it<< " " ;
cout <<" }\n" << endl ;
// remove the rootelement from the heap Numbers
pop_heap(Numbers.begin(),Numbers.end()) ;
cout <<"After calling pop_heap\n" << endl ;
// print content ofNumbers
cout <<"Numbers { " ;
for(it =Numbers.begin(); it != Numbers.end(); it++)
cout << *it<< " " ;
cout <<" }\n" << endl ;
}
程序输出为:
Numbers { 4 10 70 10 30 69 96 100 }
之后调用 make_heap
Numbers { 100 30 96 10 4 69 70 10 }
之后调用 sort_heap
Numbers { 4 10 10 30 69 70 96 100 }
之后调用 push_heap 和 make_heap
Numbers { 100 69 96 30 4 70 10 10 7 }
之后调用 pop_heap
Numbers { 96 69 70 30 4 7 10 10 100 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值