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

6

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

问题描述

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

I have some VERY inefficient vpn free vpn下载 code in which many lines appear 4 times as I go vpn free through permutations with "<" vpn下载 and ">" operations and a variety of 免费vpn variables and constants. It would vpn下载 seem that there is a way to write the function once and pass in 免费vpn the operators along with 免费vpn下载 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 免费vpn use them free vpn 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 vpn free 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 免费vpn下载 函子.这两种方法都继承了 免费vpn下载 vpn下载 vpn free 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 vpn free are no equivalents to std::less and std::greater, but it is fairly trivial to create 免费vpn下载 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; };
    }

  free vpn   static Func<T, T, vpn free bool> Less<T>()
      免费vpn   where T : IComparable<T>
    {
        return 免费vpn delegate(T lhs, T rhs) { return lhs.CompareTo(rhs) vpn下载 < 0; };
    }
}

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

Note, the above code uses the vpn下载 Func<> class 免费vpn from .NET 免费vpn 3.5. If this is not acceptable, vpn下载 consider defining you 免费vpn 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()
{
   免费vpn  DoWork(std::less<int>(), vpn free 100, 200);
    DoWork(std::greater<int>(), 100, 200);
}

C# 调用示例:

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

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

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

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

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 free vpn C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter vpn下载 count mismatch 免费vpn with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
vpn free 5

没有 EndInvoke 的 C# 异步调用?
C# free vpn vpn vpn下载 free Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 vpn下载 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14