STL算法 ---------- Heap算法

来源:互联网 发布:轮播图原生js代码 编辑:程序博客网 时间:2024/05/17 09:20

---- 堆排序算法( heapsort )

2. make_heap()

2. push_heap()

3. pop_heap

4. sort_heap()

                                                       

#include <iostream>#include <algorithm>#include <vector>using namespace std;template<typename T>void Print(const T& t){for(typename T::const_iterator itr=t.begin(); itr!=t.end(); ++itr){cout<<*itr<<' ';}cout<<endl;}int main( int argc, char** argv ){vector<int> vec;for(int i=3; i<=7; ++i){vec.push_back(i);}for(int i=5; i<=9; ++i){vec.push_back(i);}for(int i=1; i<=4; ++i){vec.push_back(i);}Print(vec);make_heap(vec.begin(), vec.end());Print(vec);pop_heap(vec.begin(), vec.end());vec.pop_back();Print(vec);vec.push_back(17);push_heap(vec.begin(), vec.end());Print(vec);sort_heap(vec.begin(), vec.end());Print(vec);return 0;}


0 0
原创粉丝点击