When to use a comparator function and when to use a comparator class?

Sometimes Prateek bhaiya uses a custom comparator function. Sometimes he uses a custom comparator class with operator overloading on ( )

For example,

He used custom comparator Function while calling the sort function of STL,

He used custom comparator Class while creating his own generic search function.

How to decide what is to be used when?

@devpratik, the general rule of thumb is :-

  • if you are using a algorithmic function and want to modify internal comparisons then you use comparator functions, like :- sort(), lower_bound(), upper_bound etc,
  • If you are working with containers class and modify the internal comparison then you use comparator class like in:- priority_queue,map,set,unordered_map,unordered_set, etc…

In short don’t memorize anything just implement it ,see what uses what and use it in your program .

In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer

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.