Functor use confusion

how exactly are we using carcompare in this
and how the comparison is happening
and A.dist() < B.dist() creates a max heap
why shouldn’t it be a min heap
as we are returning the closer distance as true

STL uses stack for storing the values based on comparison. So here your comparision A.dist()< B.dist() will return true for closed distance, as you mentioned(which is correct) and hence first smaller value will be pushed to stack then higher value.
So finally the highest value will be at the top and when you pop the values one by one it will be decreasing in order not increasing, this is how comparator works in STL. So this will build the max-heap not min-heap.

1 Like

ok sir i understood why it is a max heap and not a min heap but i am still not getting
how carcompare is being called and how it is working

it is a comparator but how it is actually working?

This is used for normally comparison only. For example when you are placing the integers in any order you are comparing, whether a<b or not. So by defining your own comparator you are defining new meaning of (a<b) meaning you are replacing ‘<’ operator by your own comparison operator.
all other things remains the same.

sir i want to know how operator () worked on it own
normal comparator like a<b we make a function
bool compare(int a , int b)
{ if(minheap) return a<b;
else return a >b;}

but here it is an operator being overloaded how how it is getting called
i am just getting confused

We are writing our own comparator function and then passing it as argument. Inside STL this function is replaced at the place of ‘<’ operator. Inside the STL the ‘<’ is overloaded and its function body contains an lambda functions whose body can be defined by user and passed as argument.

ok
got it
thank u sir