DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

Custom compare class

If marks of two student is equal give priority to Roll or Math marks or Biology marks we have to tell it to computer .

include

using namespace std;
class Student{
public:
string name ;
int roll ;
int marks ;
Student(string name , int roll , int marks){
this->name = name ;
this->roll =roll ;
this->marks = marks ;
}
};

class cmp{
public:
bool operator()(Student l , Student r )
{
if(l.marks < r.marks)
return true ;
else
return false ;

}
Enter fullscreen mode Exit fullscreen mode

};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
priority_queue,cmp> pq;
int n ;
cin >> n ;
for(int i = 0 ; i < n ; i++){
string name ;
int roll , marks ;
cin >> name >> roll >> marks ;
Student obj(name,roll , marks) ;
pq.push(obj) ;
}

while(!pq.empty()){
cout << pq.top().name << " " << pq.top().roll << " " << pq.top().marks() << endl ;

}
return 0 ;

}

Top comments (0)