问题描述
我有一些非常低效的代码,当我使用<"进行排列时,很多行出现了 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::less 和 std::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::less 和 std::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).
这篇关于将运算符与其他参数一起传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司免费vpn源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构免费vpn源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)