C ++:为std :: sort提供模板化的比较函数 [英] C++: Supplying a templated compare function to std::sort

查看:89
本文介绍了C ++:为std :: sort提供模板化的比较函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想获取std :: sort,以根据指针所指向的int的值对指向int的指针的向量进行排序.忽略那里明显的性能问题.简单吧? 发挥作用:

Suppose I want to get std::sort to sort a vector of pointers to int's, based on the value of the int's the pointers point to. Ignore the obvious performance issue there. Simple huh? Make a function:

bool sort_helper(const int *a, const int *b) 
{   
    return *a < *b;
}   

并提供给std :: sort.

and supply to the std::sort.

现在,如果我们还想使用指向大型对象的指针向量来做同样的事情.同样的情况适用: 首先我们在对象中定义一个<运算符,然后按照以下几行制作一个函数:

Now, if we also want to do the same thing with a vector of pointers to large objects. The same thing applies: first we define a < operator in the object, then make a function along the following lines:

bool sort_helper(const ob_type *a, const ob_type *b) 
{   
    return *a < *b;
}   

或其他任何东西,提供给std :: sort.

or whatever, supply that to std::sort.

现在,这就是棘手的地方:如果我们想使用任意比较函数对指向任何类型的指针的向量进行排序(我们假设与该函数一起使用的任何类型都将能够使用它)-提供上述sort_helper函数的模板版本很容易:

Now, and this is where it gets tricky: what if we want to sort a vector of pointers to any type, with any arbitrary compare function (we make the assumption that whatever type we use that function with, will be able to work with it)- supplying a template version of the sort_helper function above is easy:

template <class ob_type>
bool sort_helper(const ob_type *a, const ob_type *b) 
{   
    return *a < *b;
}   

但是,提供任意比较功能比较困难:类似这样的

However, supplying an arbitrary compare function is harder: Something like this-

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>);
}

是我想做的,但是要这样做:

is what I'd like to do, but doing this:

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的任意比较函数,根据所指向的对象的值,对指向已知类型的指针的向量进行排序?假设我在这里介绍的测试用例是实际场景的疯狂简化版本,并且有充分的理由想要以这种方式进行操作,这将花费很长时间才能分散问题的注意力.

Doesn't work. How can I sort a vector of pointers to a known type, by the value of the objects pointed to, using an arbitrary comparison function, supplied to std::sort? Assume that the test case I am presenting here is an insanely simplified version of the actual scenario, and that there are valid reasons for wanting to do things this way, that would take too long to go into and distract from the problem.

[出于各种原因,我正在寻找一种适用于C ++ 03的解决方案-感谢Nir的C ++ 14回答.

推荐答案

您不能在c ++中传递函数模板指针,您可以做的是使用operator()模板创建函子(这与具有auto参数的lambda非常相似):

You cannot pass function template pointer in c++, what you can do is to create functor with operator() template (something very similar to lambda with auto parameters):

#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>()));
}

[实时演示]

请记住,运算符必须具有const限定符,才能使调用者能够从临时对象访问它.

Remember that operator must have const qualifier to make caller to be able to access to it from temporary object.

这篇关于C ++:为std :: sort提供模板化的比较函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆