c++ 判断给定区间是否是一个heap. O(N) (is_heap)
#include <iostream> // cout #include <algorithm> // is_heap, make_heap, pop_heap #include <vector> // vector using namespace std; int main () { vector<int> foo {9,5,2,6,4,1,3,8,7}; if (!is_heap(foo.begin(),foo.end())) make_heap(foo.begin(),foo.end()); cout << "Popping out elements:"; while (!foo.empty()) { pop_heap(foo.begin(),foo.end()); // moves largest element to back cout << ' ' << foo.back(); // prints back foo.pop_back(); // pops element out of container } cout << '\n'; return 0; }