如何从构造函数传递到成员函数的排序谓词 [英] How to pass to sort predicate that is a member function, from the constructor

查看:174
本文介绍了如何从构造函数传递到成员函数的排序谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 模板< class T& 
class GenericGeneticSolver
{
public:
GenericGeneticSolver(IGenticSolverHelper& helper,int generationSize):mSolverHelper(helper),mGenerationSize(generationSize)
{
mCurrentGeneration.resize(mGenerationSize);
for(int i = 0; i {
mSolverHelper.GenerateRandomSolution(mCurrentGeneration [i]);
}

sort(mCurrentGeneration.begin(),mCurrentGeneration.end(),solutionComparer);
}
void Evolve(int numberOfGenerations = 1)
{
//sort(mCurrentGeneration.begin(),mCurrentGeneration.end(),solutionComparer);
}
private:
int mGenerationSize;
矢量< T> mCurrentGeneration;
IGenticSolverHelper< T> mSolverHelper;

bool solution Comparer(T first,T second){return(mSolverHelper.Cost(first)< mSolverHelper.Cost(second)); }
};

构造函数向量与成员,然后我试图通过传递一个谓词到排序函数,该谓词是一个成员函数名为`solutionComparer。



不幸的是,它不编译,编译器不满意在构造函数中使用成员函数的指针,我试着在Evolve函数中的同一行,它会编译。 / p>

我得到的错误是:

 错误C3867:'GenericGeneticSolver< T> :: solutionComparer':函数调用缺少参数列表;使用& GenericGeneticSolver< T> :: solutionComparer'创建指向成员的指针

做什么错误建议,但它没有编译(排序函数中的一些随机错误)。



为什么我不能使用指针 c> std :: sort

$

>需要一个比较器,可以简单地调用 compare(a,b)。 A(指向a)的成员函数是不合适的,因为它需要一个对象被调用,所以你需要一个包装器将成员函数绑定到一个对象,并使它可调用只有两个值进行比较。



在C ++ 11中,您可以将成员函数绑定到对象:

  sort(mCurrentGeneration.begin(),mCurrentGeneration.end(),
std :: bind(& GenericGeneticSolver :: solutionComparer,this,
std :: placeholders :: _ 1,std :: placeholder :: _ 2));

或您可以使用lambda:

  sort(mCurrentGeneration.begin(),mCurrentGeneration.end(),
[this](T first,T second){return solutionComparer(first,second);});

历史上,你必须制作自己的函子,

  struct SolutionComparer {
IGenticSolverHelper< T> * helper;

SolutionComparer(IGenticSolverHelper< T& helper):helper(& helper){}

bool operator()(T first,T second){
return helper-> Cost(first)< helper-> Cost(second);
}
};

sort(mCurrentGeneration.begin(),mCurrentGeneration.end(),
SolutionComparer(mSolverHelper));


I have the following code :

template <class T>
class GenericGeneticSolver
{
public:
    GenericGeneticSolver(IGenticSolverHelper<T>& helper, int generationSize) : mSolverHelper(helper), mGenerationSize(generationSize)
    {
        mCurrentGeneration.resize(mGenerationSize);
        for(int i=0;i<mGenerationSize;i++)
        {
            mSolverHelper.GenerateRandomSolution(mCurrentGeneration[i]);
        }

        sort(mCurrentGeneration.begin(),mCurrentGeneration.end(), solutionComparer);
    }
    void Evolve(int numberOfGenerations = 1)
    {
        //sort(mCurrentGeneration.begin(),mCurrentGeneration.end(), solutionComparer);
    }
private :
    int mGenerationSize;
    vector<T> mCurrentGeneration;
    IGenticSolverHelper<T>& mSolverHelper;

    bool solutionComparer (T first,T second) { return (mSolverHelper.Cost(first)<mSolverHelper.Cost(second)); }
};

In the constructor I'm filling a vector with members, and then I'm trying to sort this vector by passing a predicate to the Sort function, the predicate is a member function called `solutionComparer.

Unfortunately it does not compile, the compiler is not happy with using pointer to member functions in the constructor, i tried the same line in the "Evolve" function, and it does compile.

The error i get is :

error C3867: 'GenericGeneticSolver<T>::solutionComparer': function call missing argument list; use '&GenericGeneticSolver<T>::solutionComparer' to create a pointer to member

I tried to do what the error suggested but it didn't compile either (some random error in the sort function).

Why can't i use pointer to a member function in the constructor ?

解决方案

std::sort requires a comparator which can simply be called as compare(a,b). A (pointer to a) member function isn't suitable, since it requires an object to be called on, so you'll need a wrapper to bind the member function to an object and make it callable with just the two values to compare.

In C++11, you can bind a member function to an object:

sort(mCurrentGeneration.begin(),mCurrentGeneration.end(),
    std::bind(&GenericGeneticSolver::solutionComparer, this, 
         std::placeholders::_1, std::placeholders::_2));

or you can use a lambda:

sort(mCurrentGeneration.begin(),mCurrentGeneration.end(),
    [this](T first,T second) { return solutionComparer(first, second); });

Historically, you would have to make your own functor, perhaps along the lines of:

struct SolutionComparer {
    IGenticSolverHelper<T>* helper;

    SolutionComparer(IGenticSolverHelper<T>& helper) : helper(&helper) {}

    bool operator()(T first,T second) {
        return helper->Cost(first) < helper->Cost(second);
    }
};

sort(mCurrentGeneration.begin(),mCurrentGeneration.end(),
    SolutionComparer(mSolverHelper));

这篇关于如何从构造函数传递到成员函数的排序谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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