https://ide.codingblocks.com/s/45759
In class, Prateek bhaiya showed how to write a custom comparator for heap using class and function overloading.
He also added that instead of class we can write a bool function. But I wasn’t able to compile with “bool customComparator”.
Please debug the code.
Heap Custom Comparator
Hey Nipun, there are some compilation errors in your program in line 19: should be while(pq.size()!=0)
, here size is function you can’t use as a class member. One more thing is if you don’t want to make compare a class and want to make it a function, you can use the syntax as shown in the code snippet.
class Foo
{
};
bool Compare(Foo, Foo)
{
return true;
}
int main()
{
std::priority_queue<Foo, std::vector<Foo>, std::function<bool(Foo, Foo)>> pq(Compare);
return 0;
}