将参数传递给比较函数?

发布于 2024-09-29 21:08:16 字数 758 浏览 0 评论 0原文

当对向量使用 STL 排序算法时,我想传入我自己的比较函数,该函数也带有一个参数。

例如,理想情况下我想做一个本地函数声明,例如:

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

但是,编译器对此有所抱怨。当我尝试类似的操作时:

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

编译器仍然抱怨:“错误:使用包含函数的参数”

我该怎么办?我应该使用全局比较函数创建一些全局变量吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸里的那抹悲凉 2024-10-06 21:08:21

在 C++ 中,不能在另一个函数中定义自由函数。所以你的第一个代码片段格式不正确。

排序(v.begin(), v.end(), Local::Compare);

第三个参数必须是函数对象。在类中重载 () 运算符,然后创建函数对象。


在 C++0x 中,您可以使用 lambda 表达式

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

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

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

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);
小姐丶请自重 2024-10-06 21:08:21

一种可能性是在构造比较器对象时传递参数:

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

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));
}
离鸿 2024-10-06 21:08:21

//使用std::bind

//示例

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

//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)
    );
}
别忘他 2024-10-06 21:08:20

您无法从本地定义的函数中访问函数的本地变量 - 当前形式的 C++ 不允许 关闭。该语言的下一个版本 C++0x 将支持这一点,但该语言标准尚未最终确定,目前对当前草案标准的支持很少。

要实现此功能,您应该将 std::sort 的第三个参数更改为对象实例而不是函数。 std::sort 的第三个参数可以是任何可调用的参数(即任何 x,其中添加像 x(y, z) 这样的括号使得句法意义)。最好的方法是定义一个实现 operator() 函数的结构,然后传递该对象的实例:

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

    int paramA;
};

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

请注意,我们必须将 paramA 存储在结构体,因为我们无法从 operator() 内部访问它。

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文