Is there an easy way to make a min heap in C++?
问题 I'm very new to C++, and I was wondering if there was a way to make a min heap in C++ from the standard library. 回答1: Use make_heap() and friends, defined in <algorithm> , or use priority_queue , defined in <queue> . The priority_queue uses make_heap and friends underneath. #include <queue> // functional,iostream,ctime,cstdlib using namespace std; int main(int argc, char* argv[]) { srand(time(0)); priority_queue<int,vector<int>,greater<int> > q; for( int i = 0; i != 10; ++i ) q.push(rand()%10