Priority Queue STL - II Functors

instead of creating a class and object , cant we simplly create a carcompare function?

Hello @Shreya-Gupta-2383169445069382,

The reason to use a functor over function is to use operator overloading to define the priority.
The reason of using operator overloading is to automatically apply the sorting logic during the time when an element is entered to the priority_queue which is not possible in the case of normal function.

A functor (or function object) is a C++ class that acts like a function. Functors are called using the same old function call syntax. To create a functor, we create a object that overloads the operator().

Hope, this would help.
Give a like if you are satisfied.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.