Comparator class

Can you list down the instances where we use comparator function and where we have to use comparator class?
In this code, we used class but in sort function we pass comparator function.

Hi this totally depends on function declaration when we have to use comparator function and where we have to use comparator class
Some popular application of comparator class are : - (sorted on usage based on my experience)

  1. priority_queue STL
  2. set STL
  3. multiset STL
  4. lower_bound
  5. upper_bound

comparator function is primarily used in sort function

In case of any doubt feel free to ask :slight_smile:
mark your doubt as resolved If you got the answer

1 Like

Thanks for the answer.
I guess in the list in all the cases the comparator class will have the same structure right?

actually in some cases the comparisons differ like return a>b should be return a<b . for that you need to check it using some test cases

1 Like

Thanks.
Yes. That will change.
I just wanted to confirm the constructor structure remains the same or not.