将运算符与其他参数一起传递

Passing an operator along with other parameters(将运算符与其他参数一起传递)

本文介绍了将运算符与其他参数一起传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非常低效的代码,当我使用<"进行排列时,很多行出现了 4 次和>"操作以及各种变量和常量.似乎有一种方法可以编写一次函数并传入运算符以及必然变化的值和ref"变量.我必须学习什么技术?有人建议使用代表",但我不知道如何以这种方式使用它们.这是在 C# 2.0 VS2005 中,但如果该技术是通用的并且也可以与 C++ 一起使用,那就太好了.

I have some VERY inefficient code in which many lines appear 4 times as I go through permutations with "<" and ">" operations and a variety of variables and constants. It would seem that there is a way to write the function once and pass in the operators along with the necessarily changing values and"ref" variables. What technique do I have to learn? "Delegates" have been suggested but I don't see how to use them in this manner. This is in C# 2.0, VS2005, but if the technique is generic and can be used with C++ too, that would be great.

请求一些代码:以下以多种形式出现,带有不同的<"和>"符号以及+"和-"符号的混合:

Request for some code: The following appears in many guises, with different "<" and ">" signs as well as a mix of "+" and "-" signs:

if (move[check].Ypos - move[check].height / 200.0D < LayoutManager.VISIO_HEIGHT - lcac_c.top)
{
  move[check].Ypos = move[check].Ypos + adjust;
.
.
.

推荐答案

在 C++ 中,使用 std::lessstd::greater 函子.这两种方法都继承了 std::binary_function,所以你的泛型函数应该接受这种类型的实例.

In C++, use the std::less and std::greater functors. Both of these methods inherit std::binary_function, so your generic function should accept instances of this type.

在 .NET 中,与 std::binary_function 等效的是 Func.std::lessstd::greater 没有等价物,但创建它们相当简单.请参阅以下示例.

In .NET, the equivalent to std::binary_function is Func<T, U, R>. There are no equivalents to std::less and std::greater, but it is fairly trivial to create them. See the following example.

static class Functor
{
    static Func<T, T, bool> Greater<T>()
        where T : IComparable<T>
    {
        return delegate(T lhs, T rhs) { return lhs.CompareTo(rhs) > 0; };
    }

    static Func<T, T, bool> Less<T>()
        where T : IComparable<T>
    {
        return delegate(T lhs, T rhs) { return lhs.CompareTo(rhs) < 0; };
    }
}

注意,上面的代码使用了 .NET 3.5 中的 Func<> 类.如果这不可接受,请考虑定义您自己的委托.

Note, the above code uses the Func<> class from .NET 3.5. If this is not acceptable, consider defining you own delegate.

C++ 调用示例:

void DoWork(const std::binary_function<int, int, bool>& myOperator,
            int arg1, int arg2)
{
    if (myOperator(arg1, arg2)) { /* perform rest of work */ }
}

void main()
{
    DoWork(std::less<int>(), 100, 200);
    DoWork(std::greater<int>(), 100, 200);
}

C# 调用示例:

void DoWork(Func<int, int, bool> myOperator, int arg1, int arg2)
{
    if (myOperator(arg1, arg2)) { /* perform rest of work */ }
}

void main()
{
    DoWork(Functor.Less<int>(), 100, 200);
    DoWork(Functor.Greater<int>(), 100, 200);
}

编辑:我将仿函数类的示例更正为应用 <或 > 泛型类型的运算符不起作用(与使用 C++ 模板的方式相同).

EDIT: I corrected the example of the functor class as applying < or > operators to a generic type doesn't work (in the same manner as it does with C++ templates).

这篇关于将运算符与其他参数一起传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将运算符与其他参数一起传递

基础教程推荐