STL演算法 ---------- Heap演算法
阿新 • • 發佈:2018-12-30
---- 堆排序演算法( 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; }