1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <algorithm>
int main()
{
struct my_class { int x ; int y ; };
// custom predicate to compare two objects of type my_object
// return true if object a should appear before object b in the sorted sequence
const auto cmp_my_class = [] ( my_class a, my_class b ) { return (a.x+a.y) < (b.x+b.y) ; } ;
my_class my_objects[] { {1,8}, {4,8}, {2,5}, {5,9}, {1,9}, {2,7}, {5,6}, {2,9} } ;
// use custom predicate to compare objects for sorting
std::sort( std::begin(my_objects), std::end(my_objects), cmp_my_class ) ;
for( const auto& object : my_objects ) std::cout << '{' << object.x << ',' << object.y << "} " ;
std::cout << '\n' ;
}
|