【问题标题】:Use of a functor on for_each在 for_each 上使用函子
【发布时间】:2011-03-12 22:37:17
【问题描述】:

为什么对仿函数的for_each 调用没有在最后更新sum::total?

struct sum
{
    sum():total(0){};
    int total;

    void operator()(int element) 
    { 
       total+=element; 
    }
};

int main()
{
    sum s;

    int arr[] = {0, 1, 2, 3, 4, 5};
    std::for_each(arr, arr+6, s);
    cout << s.total << endl; // prints total = 0;
}

【问题讨论】:

    标签: c++ stl functor


    【解决方案1】:

    for_each 按值获取函子 - 所以它被复制了。你可以例如使用一个用指向外部 int 的指针初始化的仿函数。

    struct sum
    {
        sum(int * t):total(t){};
        int * total;
    
        void operator()(int element)
        {
           *total+=element;
        }
    };
    
    int main()
    {
        int total = 0;
        sum s(&total);
    
        int arr[] = {0, 1, 2, 3, 4, 5};
        std::for_each(arr, arr+6, s);
        cout << total << endl; // prints total = 15;
    }
    

    或者你可以使用for_each的返回值

    struct sum
    {
        sum():total(0){};
        int total;
    
        void operator()(int element) 
        { 
           total+=element; 
        }
    };
    
    int main()
    {
        sum s;
    
        int arr[] = {0, 1, 2, 3, 4, 5};
        s = std::for_each(arr, arr+6, s);
        cout << s.total << endl; // prints total = 15;
    }
    

    【讨论】:

      【解决方案2】:

      for_each 按值接收仿函数的副本。即使在那之后,它也可以免费复制它,但确实会返回一个副本。

      OTOH,您只是在尝试重新发明std::accumulate,这样会更容易完成这项工作:

      int total = std::accumulate(arr, arr+6, 0);
      cout << total << endl; 
      

      【讨论】:

        【解决方案3】:

        因为您传递给for_each 的s 是按值传递的。 for_each 按值接受!

        在C++0x中,可以用for_each作为解决这个问题,

        int sum  = 0;
        std::for_each(arr, arr+6, [&](int n){ sum += n; });
        std::cout << sum ;
        

        输出:

        15
        

        ideone 演示:http://ideone.com/s7OOn


        或者您可以简单地写在std::cout 本身:

        std::cout<<std::for_each(arr,arr+6,[&](int n)->int{sum += n;return sum;})(0);
        

        运行:http://ideone.com/7Hyla

        注意这种不同的语法对于学习目的是可以的,至于std::for_each 的工作原理以及它返回的内容,但我不推荐在实际代码中使用这种语法。 :-)


        在 C++ 中,您可以在函子中编写用户定义的转换函数,

        struct add
        {
            int total;
            add():total(0){};
            void operator()(int element)  {  total+=element;  }
            operator int() { return total ; }
        };
        
        int main()
        {
            int arr[] = {0, 1, 2, 3, 4, 5};
            int sum = std::for_each(arr, arr+6, add());
            std::cout << sum;
        }
        

        它与 Erik 第二种解决方案的版本略有不同:http://ideone.com/vKnmA

        【讨论】:

          【解决方案4】:

          这是因为 std::for_each 要求函子按值传递。 您的解决方案的解决方法:

          struct sum
          {
              sum():total(0){};
              int total;
              sum(sum & temp)
              {
                  total = temp.total;
              }
              void operator()(int element) 
              { 
                 total+=element; 
              }
          };
          
          int main()
          {
              sum s;
          
              int arr[] = {0, 1, 2, 3, 4, 5};
              s = std::for_each(arr, arr+6, s);  // result of for_each assigned back to s
              cout << s.total << endl; // prints total = 0;
          }
          

          【讨论】:

            猜你喜欢
            • 2010-09-18
            • 2011-02-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多