stl push_heap,make_heap,pop_heap,sort_heap的使用

stl中的堆默认是最大堆,要想用最小堆的话,必须要在push_heap,pop_heap,make_heap等每一个函数后面加第三个参数greater<int>(),括号不能省略。


1、make_heap:使序列变成堆

原型:
template <class RandomAccessIterator>
  void make_heap ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void make_heap ( RandomAccessIterator first, RandomAccessIterator last,
                   Compare comp );

范例:

[cpp]  view plain  copy
  1. // range heap example  
  2. #include <iostream>  
  3. #include <algorithm>  
  4. #include <vector>  
  5. using namespace std;  
  6.   
  7. int main () {  
  8.   int myints[] = {10,20,30,5,15};  
  9.   vector<int> v(myints,myints+5);  
  10.   
  11.   make_heap (v.begin(),v.end());  
  12.   cout << "initial max heap   : " << v.front() << endl;  
  13.   
  14.   pop_heap (v.begin(),v.end()); v.pop_back();  
  15.   cout << "max heap after pop : " << v.front() << endl;  
  16.   
  17.   v.push_back(99); push_heap (v.begin(),v.end());  
  18.   cout << "max heap after push: " << v.front() << endl;  
  19.   
  20.   sort_heap (v.begin(),v.end());  
  21.   
  22.   cout << "final sorted range :";  
  23.   for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];  
  24.   
  25.   cout << endl;  
  26.   
  27.   return 0;  
  28. }  

2、push_heap:压栈(入栈)
原型:
template <class RandomAccessIterator>
  void push_heap ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void push_heap ( RandomAccessIterator first, RandomAccessIterator last,
                   Compare comp );


3、pop_heap弹栈(出栈)

原型:
template <class RandomAccessIterator>
  void pop_heap ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void pop_heap ( RandomAccessIterator first, RandomAccessIterator last,
                   Compare comp );

范例:

[cpp]  view plain  copy
  1. // range heap example  
  2. #include <iostream>  
  3. #include <algorithm>  
  4. #include <vector>  
  5. using namespace std;  
  6.   
  7. int main () {  
  8.   int myints[] = {10,20,30,5,15};  
  9.   vector<int> v(myints,myints+5);  
  10.   vector<int>::iterator it;  
  11.   
  12.   make_heap (v.begin(),v.end());  
  13.   cout << "initial max heap   : " << v.front() << endl;  
  14.   
  15.   pop_heap (v.begin(),v.end()); v.pop_back();  
  16.   cout << "max heap after pop : " << v.front() << endl;  
  17.   
  18.   v.push_back(99); push_heap (v.begin(),v.end());  
  19.   cout << "max heap after push: " << v.front() << endl;  
  20.   
  21.   sort_heap (v.begin(),v.end());  
  22.   
  23.   cout << "final sorted range :";  
  24.   for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];  
  25.   
  26.   cout << endl;  
  27.   
  28.   return 0;  
  29. }  

4、 sort_heap 对堆排序

原型:template <class RandomAccessIterator>
  void sort_heap ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void sort_heap ( RandomAccessIterator first, RandomAccessIterator last,
                   Compare comp );

范例:

[cpp]  view plain  copy
  1. // range heap example  
  2. #include <iostream>  
  3. #include <algorithm>  
  4. #include <vector>  
  5. using namespace std;  
  6.   
  7. int main () {  
  8.   int myints[] = {10,20,30,5,15};  
  9.   vector<int> v(myints,myints+5);  
  10.   vector<int>::iterator it;  
  11.   
  12.   make_heap (v.begin(),v.end());  
  13.   cout << "initial max heap   : " << v.front() << endl;  
  14.   
  15.   pop_heap (v.begin(),v.end()); v.pop_back();  
  16.   cout << "max heap after pop : " << v.front() << endl;  
  17.   
  18.   v.push_back(99); push_heap (v.begin(),v.end());  
  19.   cout << "max heap after push: " << v.front() << endl;  
  20.   
  21.   sort_heap (v.begin(),v.end());  
  22.   
  23.   cout << "final sorted range :";  
  24.   for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];  
  25.   
  26.   cout << endl;  
  27.   
  28.   return 0;  
  29. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值