【发布时间】:2013-11-28 15:16:25
【问题描述】:
我只是在 C++ 中试验 make_heap() 函数。以下是我在每次调用 bool predicate() 时通过打印它们来跟踪正在比较的元素的代码。
#include <iostream>
#include <algorithm>
using namespace std;
// bool predicate function to make a min heap
bool predicate(int a, int b){
cout << a << " " << b << endl;
if(a >= b){ return 1; }
return 0;
}
int main(){
int arr[] = {3,2,1,-1,-2};
make_heap(arr, arr+5, predicate);
return 0;
}
我得到的输出是:
-2 -1
-2 2
1 -2
2 -1
-1 3
但我期待以下输出,同时考虑标准算法:
-2 -1
-2 2
1 -2
3 -2
2 -1
-1 3
有什么帮助吗?
【问题讨论】:
-
您拥有 您的 实现的
make_heap的模板源。有什么东西阻止你通过它吗? -
C++ 实现的
make_heap通过最少的比较创建了堆,你想让它做更多的比较吗?
标签: c++ algorithm stl heap predicates