c++ for_each() and object functions

前端 未结 10 814
盖世英雄少女心
盖世英雄少女心 2021-01-14 05:31

I have an assignment that is the following:

For a given integer array, find the sum of its elements and print out the final result, but to get the sum, you need to e

10条回答
  •  离开以前
    2021-01-14 05:44

    I know you were told to use for_each, but I would actually do this - perhaps an alternative for extra credit ;-)

    #include 
    using namespace std;
    
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    
    int sum = accumulate(array, array + (sizeof(array) / sizeof(int)), 0);
    

    accumulate is expressly designed for summing the elements of an iterable range.

提交回复
热议问题