有4个函数(make_heap, push_heap, pop_heap, sort_heap)
template<class RanIt, class Pred> void make_heap(RanIt first, RanIt last, Pred pr);
template<class RanIt, class Pred> void push_heap(RanIt first, RanIt last, Pred pr);
template<class RanIt, class Pred> void pop_heap(RanIt first, RanIt last, Pred pr);
template<class RanIt, class Pred> void sort_heap(RanIt first, RanIt last, Pred pr);
<pre name="code" class="cpp">///////////////////////////////////////////////////////////// #include "stdafx.h" #include <algorithm> #include <numeric> #include <functional> #include <vector> #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { int myints[] = {10, 20, 30, 5, 15}; std::vector<int> v(myints, myints + 5); // 10, 20, 30, 5, 15 // 生成栈 std::make_heap (v.begin(),v.end()); // 30, 20, 10, 5, 15 // 出栈 std::pop_heap (v.begin(),v.end()); // 20, 15, 10, 5, 30 v.pop_back(); // 20, 15, 10, 5 // 入栈 v.push_back(99); // 20, 15, 10, 5, 99 std::push_heap (v.begin(),v.end()); // 99, 20, 10, 5, 15 // 入栈 v.push_back(30); // 99, 20, 10, 5, 15, 30 std::push_heap (v.begin(),v.end()); // 99, 20, 30, 5, 15, 10 std::sort_heap (v.begin(),v.end()); // 5, 10, 15, 20, 30, 99 return 0; }