accumulate


Am attempting to sum the squares of a series of integers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <functional>
#include <algorithm> 

int sq(int d) {return d*d;}

int main () {
  int numbers[]={10,20,30};
  int result;
  result = std::accumulate (numbers, numbers+3, 100, std::minus<int>());
  std::cout << "The result of 100-10-20-30 is " << result << ".\n";

  result = std::accumulate (numbers, numbers+3, sq);
  std::cout << "The result of squares is " << result << ".\n";
  return 0;
}


I get the following compile errors however:
1
2
3
4
5
6
c++ -g -std=c++14 -pedantic -Wall -Wpointer-arith -Wwrite-strings -Wcast-qual -Wcast-align -Wformat-security -Wformat-nonliteral -Wmissing-format-attribute -Winline -funsigned-char "test_accumulate.cpp" (in directory: /c++/learn_c++)
test_accumulate.cpp: In function ‘int main()’:
test_accumulate.cpp:13:28: error: invalid conversion from ‘int (*)(int)’ to ‘int’ [-fpermissive]
   result = std::accumulate (numbers, numbers+3, sq);
                            ^
Compilation failed.


Can anybody suggest why?
See
http://www.cplusplus.com/reference/numeric/accumulate/?kw=accumulate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <functional>
#include <numeric>              // <==== header
#include <algorithm> 

int sq(int c, int d) {return c + d*d;}    // <==== binary op

int main () {
  int numbers[]={10,20,30};
  int result;
  result = std::accumulate (numbers, numbers+3, 100, std::minus<int>());
  std::cout << "The result of 100-10-20-30 is " << result << ".\n";

  result = std::accumulate (numbers, numbers+3, 0, sq);                // <==== initial value
  std::cout << "The result of squares is " << result << ".\n";
  return 0;
}
Last edited on

thanks, I've tried that as well, shouldv'e posted that in my initial post.
I get a different compile error though:

1
2
3
4
5
6
7
8
9
10
11
12
13
c++ -g -std=c++14 -pedantic -Wall -Wpointer-arith -Wwrite-strings -Wcast-qual -Wcast-align -Wformat-security -Wformat-nonliteral -Wmissing-format-attribute -Winline -funsigned-char "test_accumulate.cpp" (in directory: /c++/learn_c++)
In file included from /usr/include/c++/5/numeric:62:0,
                 from /usr/include/c++/5/bits/random.tcc:33,
                 from /usr/include/c++/5/random:51,
                 from /usr/include/c++/5/bits/stl_algo.h:66,
                 from /usr/include/c++/5/algorithm:62,
                 from test_accumulate.cpp:3:
/usr/include/c++/5/bits/stl_numeric.h: In instantiation of ‘_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) [with _InputIterator = int*; _Tp = int; _BinaryOperation = int (*)(int)]’:
test_accumulate.cpp:15:52:   required from here
/usr/include/c++/5/bits/stl_numeric.h:154:22: error: too many arguments to function
  __init = __binary_op(__init, *__first);
                      ^
Compilation failed.


modified code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <functional>
#include <algorithm>
#include <cmath>

int sq(int d) {return d*d;}

int main () {
  int numbers[]={10,20,30};
  int result;
  result = std::accumulate (numbers, numbers+3, 100, std::minus<int>());
  std::cout << "The result of 100-10-20-30 is " << result << ".\n";

  int sample[]={3 ,21 ,98 ,203 ,17 ,9};
  result = std::accumulate (sample, sample+6, 0, sq);
  std::cout << "The result of squares is " << result << ".\n";
  return 0;
}
OP: you need to pass a function object to accumulate, not the function as such. Can also be done with lambdas:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <functional>
#include <algorithm>

template<typename T>
struct square
{
    T operator()(const T& Left, const T& Right) const
    {
        return (Left + Right*Right);
    }
};

int main () {
  int numbers[]={10,20,30};
  int result;

  result = std::accumulate (numbers, numbers+3, 0, square<int>());
  std::cout << "The result of squares function object is " << result << ".\n";


 auto lambda = [&](double Left, double Right){return Left + Right * Right; };
 result = std::accumulate (numbers, numbers+3, 0, lambda);
  std::cout << "The result of squares lambda is " << result << ".\n";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <functional>
#include <algorithm>
#include <numeric>                              // <==== header

int sq(int c, int d) {return c+d*d;}            // <==== binary op  *******

int main () {
  int numbers[]={10,20,30};
  int result;
  result = std::accumulate (numbers, numbers+3, 100, std::minus<int>());
  std::cout << "The result of 100-10-20-30 is " << result << ".\n";

  int sample[]={3 ,21 ,98 ,203 ,17 ,9};
  result = std::accumulate (sample, sample+6, 0, sq);
  std::cout << "The result of squares is " << result << ".\n";
  return 0;
}
Last edited on

thanks very much to both of you.
You're welcome. Note that in C++14 the lambda can also be generalized in the following manner:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <functional>
#include <algorithm>


int main () {
  int numbers[]={10,20,30};
  int result;

 auto lambda = [&](const auto& Left, const auto& Right){return Left + Right * Right; };
 result = std::accumulate (numbers, numbers+3, 0, lambda);
  std::cout << "The result of squares generic lambda is " << result << ".\n";
}
Topic archived. No new replies allowed.