How to sort vector using custom comparator in c++ ? |How to sort list of object
You can sort a vector of custom objects using the C++ STL function std:: sort. The sort function has an overloaded form that takes as arguments first, last, comparator. The first and last are iterators to the first and last elements of the container. The comparator is a predicate function that can be used to tell how to sort the container.
Problem statement->
You have given some input
5 arun 8 28 harshit 10 30 surya 7 26 satyam 27 6 arun 1 28
there are 5 student and you have give there name ,scolarnumber and marks respectively. student having maximum marks at the top and if two students are having same marks then the student having lexicographically smaller name comes first , if both name and marks of the student collide then student having smaller scholar number comes first.
also read-Tower of Hanoi puzzle solution using recursion complete code | total steps and print total steps with code
Solution-
As the rule has given I will simply create my class having 3 data member and i will add in a vector (list of object) and i will sort list of object.You can do it by using sort function of vector in c++.
For sorting a list of object in vector you need a custom comparator.
syntax=sort(v.begin(), v.end(),comparator);
here is my solution-
Comments
Post a Comment