C ++: providing a templated comparison function for std :: sort

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 ']

+4
3

++, , operator() (- ):

#include <algorithm>
#include <vector>
#include <cassert>

struct less {
    template <class T>
    bool operator()(T first, T second) const {
        return first < second;
    }
};

template <class Cmp>
struct cmp_ptr {
    Cmp cmp;

    cmp_ptr(Cmp cmp):cmp(cmp) { }
    cmp_ptr() { }

    template <class T>
    bool operator()(T first, T second) const {
        return cmp(*first, *second);
    }
};

template <class Iter, class Cmp>
bool is_sorted(Iter beg, Iter end, Cmp cmp) {
   Iter prev = beg;
   Iter next = beg;
   for (next++; next != end; prev++, next++) {
      if (cmp(*next, *prev)) {
         return false;
      }
   }
   return true;
}

int main() {
    std::vector<int*> v;
    v.push_back(new int(10));
    v.push_back(new int(1));
    v.push_back(new int(5));
    v.push_back(new int(7));
    v.push_back(new int(3));
    v.push_back(new int(2));
    std::sort(v.begin(), v.end(), cmp_ptr<less>());
    assert(::is_sorted(v.begin(), v.end(), cmp_ptr<less>()));
}

[live demo]

, , .

+2

: , .

template <class T, class F>
auto make_pointee_comparison(F f) {
    return [=] (T const * l, T const * r) { return f(*l, *r); };
}

T ; T, , , .

: ++ 03, lambdas. . struct:

template <class F>
struct PointeeComparisonHelper {
    PointeeComparisonHelper(F f) : m_f(f) {}

    template <class T>
    bool operator()(T const * l, T const * r) const {
        return m_f(*l, *r);
    }

    F m_f;
};

template <class F>
PointeeComparisonHelper<F> make_pointee_comparison(F f) {
    return PointeeComparisonHelper<F>(f);
}

: 03; ++ 14, 11. 03, <int> make_pointee_comparison.

:

auto c = make_pointee_comparison<int>([] (int x, int y) { return x < y; });
int x = 5;
int y = 6;
std::cerr << c(&x, &y) << c(&y, &x);

10 (true false). , , , ++. :

bool compare(int x, int y) { return x > y; }

auto c2 = make_pointee_comparison<int>(&compare);
std::cerr << c2(&x, &y) << c2(&y, &x);

:

template <typename comparison_function, class iterator_type>
void t_sort(const iterator_type &begin, const iterator_type &end, comparison_function compare)
{
    using deref_type = const decltype(*begin);
    std::sort(begin, end, make_pointee_comparison<deref_type>(compare));
}
+5

, - :

template <typename comparison_function, class iterator_type>
void sort_deref(const iterator_type &begin, const iterator_type &end, comparison_function compare) {
    std::sort(begin, end, [compare](auto *a, auto *b) { compare(*a,*b); });
}

// example usage:
sort_deref(std::begin(container), std::end(container), std::less<>());

comparison_function .

0

Source: https://habr.com/ru/post/1676800/


All Articles