x y z, . - , std::sort. , .
struct coord_comparison {
int coord_id;
bool operator()( double (*l)[3], double (*r)[3] ) {
return (*l)[ coord_id ] < (*r)[ coord_id ];
}
coord_comparison( int id ) { coord_id = id; }
};
, , , operator() . :
std::sort(points_vec.begin(), points_vec.end(), compare_points( 1 ) );
3 :
std:vector<double*> points_vec;
, double* . :
std:vector<double(*)[3]> points_vec;
std::sort - , :
bool compare_coords( double(*l)[3], double(*r)[3] ) {
, std::less:
return std::less( *l, *l + ( sizeof *l/sizeof **l ), r );
(, , , )
return std::less( *l, *l + 3, r );
}
This function can be useful outside the class, so I would make it free. You must do it staticif it stays inside the class.
Finally, leave parens without parameters when passing the function std::sort:
std::sort(points_vec.begin(), points_vec.end(), compare_points );
source
share