I have a class like this:
class student
{
public:
string name;
int age;
int rollnum;
int year;
string father;
string mother;
string category;
string region;
char sex;
string branch;
int semester;
};
I have created a vector (vector j1) of such class student, and now I want to sort them on the basis of their name.
How can I do that?
PS: I did this.
student lessthan
{
bool operator() (const student& h1, const student& h2)
{
return (h1.name < h2.name);
}
};
And then did this,
sort(j1.begin(),j1.end(),lessthan());
But this shows an error saying expected primary expression before bool.
lessthanshould be aclass/struct. typo aboutstudent?studentshould bestruct.char sex;This is not exactly type-safe. It supposedly allows for'M'and'F'. Or'm'and'f'? Or both? What if someone accidentally assigns'a'or'b', or 123 or 255 or 0? You should at least use an enum.