转载自stl的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 );
范例:
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()); 15 v.pop_back(); 16 cout << "max heap after pop : " << v.front() << endl; 17 18 v.push_back(99); 19 push_heap (v.begin(),v.end()); 20 cout << "max heap after push: " << v.front() << endl; 21 22 sort_heap (v.begin(),v.end()); 23 24 cout << "final sorted range :"; 25 for (unsigned i=0; i<v.size(); i++) cout << " " << v[i]; 26 27 cout << endl; 28 29 return 0; 30 }
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 );
范例:
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()); 16 v.pop_back(); 17 cout << "max heap after pop : " << v.front() << endl; 18 19 v.push_back(99); 20 push_heap (v.begin(),v.end()); 21 cout << "max heap after push: " << v.front() << endl; 22 23 sort_heap (v.begin(),v.end()); 24 25 cout << "final sorted range :"; 26 for (unsigned i=0; i<v.size(); i++) cout << " " << v[i]; 27 28 cout << endl; 29 30 return 0; 31 }
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 );
范例:
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()); 16 v.pop_back(); 17 cout << "max heap after pop : " << v.front() << endl; 18 19 v.push_back(99); 20 push_heap (v.begin(),v.end()); 21 cout << "max heap after push: " << v.front() << endl; 22 23 sort_heap (v.begin(),v.end()); 24 25 cout << "final sorted range :"; 26 for (unsigned i=0; i<v.size(); i++) cout << " " << v[i]; 27 28 cout << endl; 29 30 return 0; 31 }
注:例子来源于www.cplusplus.com网站