Passing a parameter to a comparison function?

倾然丶 夕夏残阳落幕 提交于 2019-12-29 11:35:12

问题


When using the STL sort algorithm on a vector, I want to pass in my own comparison function which also takes a parameter.

For example, ideally I want to do a local function declaration like:

int main() {
    vector<int> v(100);
    // initialize v with some random values

    int paramA = 4;

    bool comp(int i, int j) {
        // logic uses paramA in some way...
    }

    sort(v.begin(), v.end(), comp);
}

However, the compiler complains about that. When I try something like:

int main() {
    vector<int> v(100);
    // initialize v with some random values

    int paramA = 4;

    struct Local {
        static bool Compare(int i, int j) {
            // logic uses paramA in some way...
        }
    };

    sort(v.begin(), v.end(), Local::Compare);
}

The compiler still complains: "error: use of parameter from containing function"

What should I do? Should I make some global variables with a global comparison function..?

Thanks.


回答1:


You cannot access the local variables of a function from within a locally defined function -- C++ in its current form does not allow closures. The next version of the language, C++0x, will support this, but the language standard has not been finalized and there is little support for the current draft standard at the moment.

To make this work, you should change the third parameter of std::sort to be an object instance instead of a function. The third parameter of std::sort can be anything that is callable (i.e. any x where adding parentheses like x(y, z) makes syntactic sense). The best way to do this is to define a struct that implements the operator() function, and then pass an instance of that object:

struct Local {
    Local(int paramA) { this->paramA = paramA; }
    bool operator () (int i, int j) { ... }

    int paramA;
};

sort(v.begin(), v.end(), Local(paramA));

Note that we have to store paramA in the structure, since we can't access it otherwise from within operator().




回答2:


In C++ you cannot define a free function inside another function. So your first code snippet is ill formed.

sort(v.begin(), v.end(), Local::Compare);

The 3rd argument must be a function object. Overload () operator inside the class and then create the function object.


In C++0x you can use lambda expressions.

auto comp = [&](int m,int n)-> bool {

        return m<n; //or use paramA in some way
    };

sort(v.begin(), v.end(), comp);



回答3:


One possibility is to pass the parameter when you construct your comparator object:

class cmp {
    int param;
public:
    cmp(int p) : param(p) {}

    bool operator()(int i, int j) {
        // logic uses param
    }
};

int main() {
    vector<int> v(100);
    // initialize v with some random values

    int paramA = 4;

    sort(v.begin(), v.end(), cmp(paramA));
}



回答4:


//Using std::bind

//Example

{
vector<int> vecInt{2, 4, 10, 20, 30};
    int i = 4;
    sort(vecInt.begin(), vecInt.end(), std::bind( [](int a, int b, int c)
    {
        return abs(a - c) < abs(b - c);
    }, std::placeholders::_1, std::placeholders::_2, i)
    );
}


来源:https://stackoverflow.com/questions/4066576/passing-a-parameter-to-a-comparison-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!