These should be const reference arguments:
Code:
//compares weight
bool lighter( const Contestant& weight1 , const Contestant& weight2 ) {
return (weight1.getWeight() < weight2.getWeight()) ;}
bool heavier( const Contestant& weight1 , const Contestant& weight2 ) {
return (weight1.getWeight() > weight2.getWeight());}
All your "get" member functions should be const functions:
Code:
string getFirstName() const
{ //accessor
return first_name;
}
string getLastName() const
{ //accessor
return last_name;
}
int getHeight() const
{ //accessor
return height;
}
int getWeight() const
{ //accessor
return weight;
}
Originally Posted by
wind_lark
how do i compare the 4 different variables together with a boolean function and sort using STL? bcos i figure out can only compare 2 items at once.
You can have functions, like lighter and heavier, but that operate on all the data members of the class you need in order to sort to your desires. Say you wanted to sort by last name and then first name, you could do this.
Code:
struct sort_by_name
{
public:
bool inline operator()(const Contestant& contestant1, const Contestant& contestant2 )
{
if( contestant1.getLastName() < contestant2.getLastName() )
return true;
else if( contestant1.getLastName() == contestant2.getLastName() )
return contestant1.getFirstName() < contestant2.getFirstName();
else return false;
}
};
...
sort( v_data.begin(), v_data.end(), sort_by_name() );