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

      <tfoot id='wrMTu'></tfoot>
      <legend id='wrMTu'><style id='wrMTu'><dir id='wrMTu'><q id='wrMTu'></q></dir></style></legend>


        Why use functors over functions?(为什么在函数上使用函子?)
            <tbody id='2xRWC'></tbody>

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

              <small id='2xRWC'></small><noframes id='2xRWC'>

                <bdo id='2xRWC'></bdo><ul id='2xRWC'></ul>

                1. 本文介绍了为什么在函数上使用函子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!



                  double average = CalculateAverage(values.begin(), values.end());

                  double average = std::for_each(values.begin(), values.end(), CalculateAverage());


                  What are the benefits of using a functor over a function? Isn't the first a lot easier to read (even before the implementation is added)?


                  class CalculateAverage
                     std::size_t num;
                     double sum;
                     CalculateAverage() : num (0) , sum (0)
                     void operator () (double elem) 
                        sum += elem;
                     operator double() const
                         return sum / num;



                  At least four good reasons:


                  在您的特定示例中,基于函子的方法具有将迭代逻辑与平均计算逻辑分离的优点.因此,您可以在其他情况下使用您的函子(想想 STL 中的所有其他算法),并且您可以将其他函子与 for_each 一起使用.

                  In your particular example, the functor-based approach has the advantage of separating the iteration logic from the average-calculation logic. So you can use your functor in other situations (think about all the other algorithms in the STL), and you can use other functors with for_each.


                  您可以更轻松地参数化函子.因此,例如,您可以有一个 CalculateAverageOfPowers 函子,它取数据的平方或立方体等的平均值,可以这样写:

                  You can parameterise a functor more easily. So for instance, you could have a CalculateAverageOfPowers functor that takes the average of the squares, or cubes, etc. of your data, which would be written thus:

                  class CalculateAverageOfPowers
                      CalculateAverageOfPowers(float p) : acc(0), n(0), p(p) {}
                      void operator() (float x) { acc += pow(x, p); n++; }
                      float getAverage() const { return acc / n; }
                      float acc;
                      int   n;
                      float p;


                  You could of course do the same thing with a traditional function, but then makes it difficult to use with function pointers, because it has a different prototype to CalculateAverage.



                  And as functors can be stateful, you could do something like this:

                  CalculateAverage avg;
                  avg = std::for_each(dataA.begin(), dataA.end(), avg);
                  avg = std::for_each(dataB.begin(), dataB.end(), avg);
                  avg = std::for_each(dataC.begin(), dataC.end(), avg);


                  to average across a number of different data-sets.

                  请注意,几乎所有接受函子的 STL 算法/容器都要求它们是纯"谓词,即随着时间的推移没有可观察到的状态变化.for_each 是这方面的一个特例(参见例如 Effective Standard C++ Library - for_each vs. transforma>).

                  Note that almost all STL algorithms/containers that accept functors require them to be "pure" predicates, i.e. have no observable change in state over time. for_each is a special case in this regard (see e.g. Effective Standard C++ Library - for_each vs. transform).


                  函数通常可以被编译器内联(毕竟 STL 是一堆模板).虽然理论上函数也是如此,但编译器通常不会通过函数指针内联.典型的例子是比较 std::sortqsort;假设比较谓词本身很简单,STL 版本通常要快 5-10 倍.

                  Functors can often be inlined by the compiler (the STL is a bunch of templates, after all). Whilst the same is theoretically true of functions, compilers typically won't inline through a function pointer. The canonical example is to compare std::sort vs qsort; the STL version is often 5-10x faster, assuming the comparison predicate itself is simple.



                  Of course, it's possible to emulate the first three with traditional functions and pointers, but it becomes a great deal simpler with functors.




                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                    <legend id='0w1mj'><style id='0w1mj'><dir id='0w1mj'><q id='0w1mj'></q></dir></style></legend>

                      1. <tfoot id='0w1mj'></tfoot>
                          <tbody id='0w1mj'></tbody>

                        <small id='0w1mj'></small><noframes id='0w1mj'>

                          <bdo id='0w1mj'></bdo><ul id='0w1mj'></ul>

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