<legend id='NcEKk'><style id='NcEKk'><dir id='NcEKk'><q id='NcEKk'></q></dir></style></legend>

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

    1. <tfoot id='NcEKk'></tfoot>
    2. <small id='NcEKk'></small><noframes id='NcEKk'>

    3. 你什么时候在 C++ 中使用函数对象?

      When do you use function objects in C++?(你什么时候在 C++ 中使用函数对象?)
      • <legend id='cXwg3'><style id='cXwg3'><dir id='cXwg3'><q id='cXwg3'></q></dir></style></legend>
        <tfoot id='cXwg3'></tfoot>

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

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

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

            <tbody id='cXwg3'></tbody>

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

                问题描述

                我看到函数对象经常与 STL 算法一起使用.函数对象是不是因为这些算法而产生的?你什么时候在 C++ 中使用函数对象?它有什么好处?

                I see function objects used often together with STL algorithms. Did function objects came about because of these algorithms? When do you use a function object in C++? What is its benefits?

                推荐答案

                正如 jdv 所说,使用函子代替函数指针,函数指针更难优化和内联编译器;此外,函子的一个基本优点是它们可以轻松地在调用它们之间保持状态1,因此它们可以根据其他时间被调用而不同地工作,以某种方式跟踪它们的参数用过,...

                As said jdv, functors are used instead of function pointers, that are harder to optimize and inline for the compiler; moreover, a fundamental advantage of functors is that they can easily preserve a state between calls to them1, so they can work differently depending on the other times they have been called, keep track somehow of the parameters they have used, ...

                例如,如果您想对两个整数容器中的所有元素求和,您可以执行以下操作:

                For example, if you want to sum all the elements in two containers of ints you may do something like this:

                struct
                {
                    int sum;
                    void operator()(int element) { sum+=element; }
                } functor;
                functor.sum=0;
                functor = std::for_each(your_first_container.begin(), your_first_container.end(), functor);
                functor = std::for_each(your_second_container.begin(), your_second_container.end(), functor);
                std::cout<<"The sum of all the elements is: "<<functor.sum<<std::endl;
                

                <小时>

                1. 实际上,正如 R Samuel Klatchko 在下面指出的那样,它们可以支持多个独立状态,每个函子实例一个:
                1. Actually, as R Samuel Klatchko pointed out below, they can support multiple independent states, one for each functor instance:
                稍微更精确的说法是函子可以支持多个独立状态(函数可以通过既不是线程安全的也不是可重入的静态/全局变量).
                A slightly more precise statement is that functors can support multiple independent states (functions can support a single state via statics/globals which is neither thread-safe nor reentrant).

                函子使您能够使用更复杂的状态,例如共享状态(静态字段)和私有状态(实例字段).然而,很少使用这种进一步的灵活性.

                Functors enables you to use even more complicated states, for example a shared state (static fields) and a private state (instance fields). However this further flexibility is rarely used.

                这篇关于你什么时候在 C++ 中使用函数对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                STL BigInt class implementation(STL BigInt 类实现)
                Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                      <tbody id='oBUTr'></tbody>
                    <i id='oBUTr'><tr id='oBUTr'><dt id='oBUTr'><q id='oBUTr'><span id='oBUTr'><b id='oBUTr'><form id='oBUTr'><ins id='oBUTr'></ins><ul id='oBUTr'></ul><sub id='oBUTr'></sub></form><legend id='oBUTr'></legend><bdo id='oBUTr'><pre id='oBUTr'><center id='oBUTr'></center></pre></bdo></b><th id='oBUTr'></th></span></q></dt></tr></i><div id='oBUTr'><tfoot id='oBUTr'></tfoot><dl id='oBUTr'><fieldset id='oBUTr'></fieldset></dl></div>
                    • <bdo id='oBUTr'></bdo><ul id='oBUTr'></ul>

                      • <small id='oBUTr'></small><noframes id='oBUTr'>

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