Comparator Classes are used to compare the objects of user-defined classes. In order to develop a generic function use template, and in order to make the function more generic use containers, so that comparisons between data can be made.
Explanation: The above comparator function operator() class take two pair of objects at a time and return true if data members of the two operators are the same. There can be any condition as per the need of the problem in the comparator function. In the above example, the function returns true if data members are the same.
For implementing Linear Search on the array elements, searching an integer in a given array can be implemented easily. But searching any element on user defined data type can’t be implemented easily as in case of array. In this case, the comparator class is used to implement it. Below is the program for the same:
- In the above program, a list of student objects is made in which initially 2 student objects are inserted.
- Now to search for a particular student a template linear search function is written which makes use of the comparator class.
- The comparator class compares the student to be searched from the list of students on the basis of their name attribute.
- If the name attribute of the object to be searched is equal to any of the object’s name attribute in the list then it returns true, otherwise, it returns false.
Let us take another example of using Comparator class for sorting, suppose the task is to sort an array of objects based on its attributes value, then the idea is to create a custom comparator class in which the function on which the sorting has to be done can be mentioned. Then, it can be passed as an argument in sort() function.
Harshit Raj Prerna
- In the above program, a list of student objects is made in which initially 3 student objects are inserted.
- Now if the list of the student has to be sorted on the basis of the student’s rollno attribute then a custom comparator class has to be passed as an argument to the sort() function.
- In the comparator class, a predicate logic has to be mentioned which returns either true or false on the basis of which the list is sorted.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Comparator function of qsort() in C
- Sort an array of pairs using Java Pair and Comparator
- Count number of pairs with the given Comparator
- Sort an Array of dates in ascending order using Custom Comparator
- How to convert a class to another class type in C++?
- Difference between Base class and Derived class in C++
- std::uniform_real_ distribution class in C++ with Examples
- C++ boost::dynamic_bitset Class with Examples
- std::is_trivially_copy_assignable class in C++ with Examples
- std::bad_weak_ptr class in C++ with Examples
- std::bad_array_new_length class in C++ with Examples
- What all is inherited from parent class in C++?
- Can a C++ class have an object of self type?
- Structure vs class in C++
- Hiding of all overloaded methods with same name in base class
- Why is the size of an empty class not zero in C++?
- Simulating final class in C++
- What happens when more restrictive access is given to a derived class method in C++?
- How to make a C++ class whose objects can only be dynamically allocated?
- Can we access private data members of a class without using a member or a friend function?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.