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 );
范例:
-
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
-
- int main () {
- int myints[] = {10,20,30,5,15};
- vector<int> v(myints,myints+5);
-
- make_heap (v.begin(),v.end());
- cout << "initial max heap : " << v.front() << endl;
-
- pop_heap (v.begin(),v.end()); v.pop_back();
- cout << "max heap after pop : " << v.front() << endl;
-
- v.push_back(99); push_heap (v.begin(),v.end());
- cout << "max heap after push: " << v.front() << endl;
-
- sort_heap (v.begin(),v.end());
-
- cout << "final sorted range :";
- for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];
-
- cout << endl;
-
- return 0;
- }
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 );
范例:
-
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
-
- int main () {
- int myints[] = {10,20,30,5,15};
- vector<int> v(myints,myints+5);
- vector<int>::iterator it;
-
- make_heap (v.begin(),v.end());
- cout << "initial max heap : " << v.front() << endl;
-
- pop_heap (v.begin(),v.end()); v.pop_back();
- cout << "max heap after pop : " << v.front() << endl;
-
- v.push_back(99); push_heap (v.begin(),v.end());
- cout << "max heap after push: " << v.front() << endl;
-
- sort_heap (v.begin(),v.end());
-
- cout << "final sorted range :";
- for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];
-
- cout << endl;
-
- return 0;
- }
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 );
范例:
-
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
-
- int main () {
- int myints[] = {10,20,30,5,15};
- vector<int> v(myints,myints+5);
- vector<int>::iterator it;
-
- make_heap (v.begin(),v.end());
- cout << "initial max heap : " << v.front() << endl;
-
- pop_heap (v.begin(),v.end()); v.pop_back();
- cout << "max heap after pop : " << v.front() << endl;
-
- v.push_back(99); push_heap (v.begin(),v.end());
- cout << "max heap after push: " << v.front() << endl;
-
- sort_heap (v.begin(),v.end());
-
- cout << "final sorted range :";
- for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];
-
- cout << endl;
-
- return 0;
- }