I am trying to sort vector using sort method.
here is my code
bool compare(const student * a, const student* b)
{
return *a.mark < *b.mark || (a.mark == b.mark && a.name < b.name);
}
sort(total_student.begin(), total_student.end(), compare);
I get error before return
, which read expected a ';'
, any idea why I might be getting this error?
Either you defined the function compare
within another function or the reason of the error is another syntax error that precedes the line with the function definition.
You may not define a function within another function. Instead of the function you could define a lambda expression.
Place the function definition in a namespace.
Moreover the function definition is invalid due to incorrect member accesses. At least you should write
bool compare(const student *a, const student *b)
{
return a->mark < b->mark || (a->mark == b->mark && a->name < b->name);
}
Also it looks confusing that you have a vector of pointers. If you have a vector of objects of the type student
then the function definition will look like
bool compare(const student &a, const student &b)
{
return a.mark < b.mark || (a.mark == b.mark && a.name < b.name);
}
Or you could write
#include <tuple>
//...
bool compare(const student &a, const student &b)
{
return std::tie( a.mark, a.name ) < std::tie( b.mark, b.name );
}