Using lambdas is a more advanced technique than simply defining a comparison function. For example, using a comparison function:
// UNTESTED CODE
typedef struct
{
double a;
int b;
} MyStruct;
// Comparison function
bool myStructLess( const MyStruct & lhs, const MyStruct & rhs ) // "lhs" = "left-hand side"
{
if ( lhs.a < rhs.a )
return true;
else if ( lhs.a > rhs.a )
return false;
else
return lhs.b < rhs.b;
}
std::vector< MyStruct > myStructVector;
std::sort( myStructVector.begin(), myStructVector.end(), myStructLess() );
// UNTESTED CODE
typedef struct
{
double a;
int b;
} MyStruct;
// Comparison function
bool myStructLess( const MyStruct & lhs, const MyStruct & rhs ) // "lhs" = "left-hand side"
{
if ( lhs.a < rhs.a )
return true;
else if ( lhs.a > rhs.a )
return false;
else
return lhs.b < rhs.b;
}
std::vector< MyStruct > myStructVector;
std::sort( myStructVector.begin(), myStructVector.end(), myStructLess() );
To copy to clipboard, switch view to plain text mode
But if you understand lambdas or functors, then use whatever method works best for you.
Bookmarks