与其他参数一起传递一个操作员 [英] Passing an operator along with other parameters

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

问题描述

我有一些非常低效code,其中很多线路出现4次,因为我去通过与排列组合<和>的操作和各种变量和常量。这似乎是有一次写函数,并通过与必然改变价值观和裁判变量一起经营的一种方式。我有什么方法来学习? 代表们已经提出,但我不明白如何以这种方式使用它们。这是C#2.0,VS2005,但如果技术是通用的,可以用C ++也可以使用,那将是巨大的。

请给一些code:在许多伪装下出现,不同的<和>标志以及+和混合 - 标志:

 如果(移动[检查] .Ypos  - 移动[检查] .height / 200.0D< LayoutManager.VISIO_HEIGHT  -  lcac_c.top)
{
  移动[检查] .Ypos =举动【检查】.Ypos +调节;




解决方案

在C ++中,使用的 的std ::少 和的 的std ::更大 仿函数。这两种方法都继承 的std :: binary_function的 ,所以你的泛型函数应该接受这种类型的实例。

在.NET中,相当于的std :: binary_function的是的 Func键< T,U,R> 。有没有等同的std ::少的std ::更大,但它是相当微不足道创建它们。看下面的例子。

 静态类函子
{
    静态Func键< T,BOOL>大< T>()
        其中T:IComparable的< T>
    {
        返回委托(T LHS,T RHS){返回lhs.CompareTo(右)GT; 0; };
    }    静态Func键< T,BOOL>以下< T>()
        其中T:IComparable的< T>
    {
        返回委托(T LHS,T RHS){返回lhs.CompareTo(右)LT; 0; };
    }
}

从.NET 3.5 类;

请注意,上面的code使用 Func键<&GT。如果这是不能接受的,可以考虑使用 系统。predicate<方式>

C ++调用的例子:

 无效的DoWork(常量的std :: binary_function的< INT,INT,BOOL>&安培; myOperator,
            INT ARG1,ARG2 INT)
{
    如果(myOperator(ARG1,ARG2)){/ *执行工作*休息/}
}无效的主要()
{
    DoWork的(性病::少< INT>(),100,200);
    DoWork的(性病::更大< INT>(),100,200);
}

C#调用例如:

 无效的DoWork(Func键< INT,BOOL> myOperator,INT ARG1,ARG2 INT)
{
    如果(myOperator(ARG1,ARG2)){/ *执行工作*休息/}
}无效的主要()
{
    DoWork的(Functor.Less&所述; INT>(),100,200);
    DoWork的(Functor.Greater&所述; INT>(),100,200);
}

修改:我纠正了仿函数类的例子为适用<或>运营商泛型类型不工作(在,因为它与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;
.
.
.

解决方案

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.

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, bool> Greater<T>()
        where T : IComparable<T>
    {
        return delegate(T lhs, T rhs) { return lhs.CompareTo(rhs) > 0; };
    }

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

Note, the above code uses the Func<> class from .NET 3.5. If this is not acceptable, consider using System.Predicate<>.

C++ invocation example:

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# invocation example:

void DoWork(Func<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);
}

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

这篇关于与其他参数一起传递一个操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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