<i id='qzoyn'><tr id='qzoyn'><dt id='qzoyn'><q id='qzoyn'><span id='qzoyn'><b id='qzoyn'><form id='qzoyn'><ins id='qzoyn'></ins><ul id='qzoyn'></ul><sub id='qzoyn'></sub></form><legend id='qzoyn'></legend><bdo id='qzoyn'><pre id='qzoyn'><center id='qzoyn'></center></pre></bdo></b><th id='qzoyn'></th></span></q></dt></tr></i><div id='qzoyn'><tfoot id='qzoyn'></tfoot><dl id='qzoyn'><fieldset id='qzoyn'></fieldset></dl></div>

    <tfoot id='qzoyn'></tfoot>

    <small id='qzoyn'></small><noframes id='qzoyn'>

  • <legend id='qzoyn'><style id='qzoyn'><dir id='qzoyn'><q id='qzoyn'></q></dir></style></legend>

      <bdo id='qzoyn'></bdo><ul id='qzoyn'></ul>

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

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

        1. <i id='Hbhmo'><tr id='Hbhmo'><dt id='Hbhmo'><q id='Hbhmo'><span id='Hbhmo'><b id='Hbhmo'><form id='Hbhmo'><ins id='Hbhmo'></ins><ul id='Hbhmo'></ul><sub id='Hbhmo'></sub></form><legend id='Hbhmo'></legend><bdo id='Hbhmo'><pre id='Hbhmo'><center id='Hbhmo'></center></pre></bdo></b><th id='Hbhmo'></th></span></q></dt></tr></i><div id='Hbhmo'><tfoot id='Hbhmo'></tfoot><dl id='Hbhmo'><fieldset id='Hbhmo'></fieldset></dl></div>
              <bdo id='Hbhmo'></bdo><ul id='Hbhmo'></ul>
            • <tfoot id='Hbhmo'></tfoot>

              • <legend id='Hbhmo'><style id='Hbhmo'><dir id='Hbhmo'><q id='Hbhmo'></q></dir></style></legend>
                  <tbody id='Hbhmo'></tbody>

                <small id='Hbhmo'></small><noframes id='Hbhmo'>

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

                  问题描述

                  我有一些非常低效的代码,当我使用<"进行排列时,很多行出现了 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).

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                    <small id='3s2iV'></small><noframes id='3s2iV'>

                    • <i id='3s2iV'><tr id='3s2iV'><dt id='3s2iV'><q id='3s2iV'><span id='3s2iV'><b id='3s2iV'><form id='3s2iV'><ins id='3s2iV'></ins><ul id='3s2iV'></ul><sub id='3s2iV'></sub></form><legend id='3s2iV'></legend><bdo id='3s2iV'><pre id='3s2iV'><center id='3s2iV'></center></pre></bdo></b><th id='3s2iV'></th></span></q></dt></tr></i><div id='3s2iV'><tfoot id='3s2iV'></tfoot><dl id='3s2iV'><fieldset id='3s2iV'></fieldset></dl></div>

                        • <bdo id='3s2iV'></bdo><ul id='3s2iV'></ul>
                          <legend id='3s2iV'><style id='3s2iV'><dir id='3s2iV'><q id='3s2iV'></q></dir></style></legend>
                          • <tfoot id='3s2iV'></tfoot>

                              <tbody id='3s2iV'></tbody>