与其他参数一起传递一个操作员

编程入门 行业动态 更新时间:2024-10-26 16:27:54
本文介绍了与其他参数一起传递一个操作员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些非常低效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).

更多推荐

与其他参数一起传递一个操作员

本文发布于:2023-11-24 09:19:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作员   参数   与其他

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!