#include<iostream>
#include<algorithm>
using namespace std;
template <typename T>
bool HeapComparator (T,T);
template <typename T>
void PrintArray(T arr,int size)
{
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
int arr[7]={1,4,2,5,7,6,3};
int size=7;
PrintArray(arr,size);
cout<<"Max Heap:"<<endl;
make_heap(arr,arr+size); //creates MAX Heap, as no comparator provided.
PrintArray(arr,size);
cout<<"Min Heap:"<<endl;
make_heap(arr,arr+size,HeapComparator); //compilation error here.
PrintArray(arr,size);
return 0;
}
template <typename T>
bool HeapComparator(T a, T b)
{
if(a>b)
return false;
else
return true;
}
它不起作用。如何使用模板函数作为比较器?