Q9. Sets STL#9
Predict the output of the following code :
struct student{
string name;
float cgpa;
student(string name, float cgpa){
this -> name = name;
this -> cgpa = cgpa;
}
};
struct comp{
bool operator () (const student& x, student y) {
if ( x.name == "Wang" )
return 1;
if ( y.name == "Wang" )
return 0;
return x.name < y.name;
}
};
set < student, comp > batch ;
batch.insert(student("Wang", 8.1));
batch.insert(student("Ming", 6.32));
batch.insert(student("Bruce", 8.82));
batch.insert(student("Pandora", 7.63));
for(student i : batch)
cout<< i.cgpa << '\t ';
8.1 8.82 6.32 7.63
8.82 6.32 7.63 8.1
8.82 6.32 8.1 7.63
8.1 8.1 6.32 8.82
How comparator works in set?