Suppose I want to get std :: sort to sort a vector of pointers to int based on the int value that the pointer points to. Ignore the obvious performance issue. Easier, huh? Make a function:
bool sort_helper(const int *a, const int *b)
{
return *a < *b;
}
and put in std :: sort.
Now, if we also want to do the same with the vector of pointers to large objects. The same applies: first we define the operator <in the object, then we execute the function on the following lines:
bool sort_helper(const ob_type *a, const ob_type *b)
{
return *a < *b;
}
or something else, put this on std :: sort.
, : , ( , , , ) - sort_helper :
template <class ob_type>
bool sort_helper(const ob_type *a, const ob_type *b)
{
return *a < *b;
}
, : - -
template <typename comparison_function, class ob_type>
bool sort_template_helper(const ob_type *a, const ob_type *b)
{
return comparison_function(*a, *b);
}
template <typename comparison_function, class iterator_type>
void t_sort(const iterator_type &begin, const iterator_type &end, comparison_function compare)
{
std::sort(begin, end, sort_template_helper<compare>);
}
- , , :
bool less_than(const int a, const int b)
{
return a < b;
}
void do_stuff()
{
t_sort(ipoint_vector.begin(), ipoint_vector.end(), sort_template_helper<less_than>);
}
.
, , , std:: sort? , , , , , - , , .
[EDIT: , ++ 03 - Nir ++ 14 tho ']