#include <stdio.h>
#include <vector>
#include <numeric>
using namespace std;
int sum_o(int s, int n)
{
return (n % 2 == 0) ? s + n : s;
}
int sum(int x, int y)
{
return x + y;
}
int mul(int x, int y)
{
return x * y;
}
int main()
{
// 初期設定
vector<int> v1 {1, 2, 3, 4, 5};
printf("初期状態 v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
// accumulate
int s1 = accumulate(v1.begin(), v1.end(), 0);
// int s1 = accumulate(v1.begin(), v1.end(), 0, sum); // 関数
// int s1 = accumulate(v1.begin(), v1.end(), 0, plus<>()); // 二項関数オブジェクト plus
// int s1 = accumulate(v1.begin(), v1.end(), 0, [](int s, int x){ return s += x; }); // ラムダ式
int s2 = accumulate(v1.begin(), v1.end(), 0, sum_o);
// int s2 = accumulate(v1.begin(), v1.end(), 0, [](int s, int n){ return (n % 2 == 0) ? s + n : s; });
printf(" すべての要素の和(accumulate) = %d\n", s1);
printf(" 偶数要素の和(accumulate) = %d\n", s2);
// reduce
s1 = reduce(v1.begin(), v1.end(), 0);
// s1 = reduce(v1.begin(), v1.end());
// s1 = reduce(v1.begin(), v1.end(), 0, sum);
// s1 = reduce(v1.begin(), v1.end(), 0, plus<>());
s2 = reduce(v1.begin(), v1.end(), 0, sum_o);
// s2 = reduce(v1.begin(), v1.end(), 0, [](int s, int n){ return (n % 2 == 0) ? s + n : s; });
printf(" すべての要素の和(reduce) = %d\n", s1);
printf(" 偶数要素の和(reduce) = %d\n", s2);
// transform_reduce, inner_product
vector<int> v2 {0, 1, 2, 3, 4};
printf("初期状態 v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
printf("初期状態 v2 :");
for (auto x : v2)
printf(" %d", x);
printf("\n");
s1 = transform_reduce(v1.begin(), v1.end(), v2.begin(), 0);
// s1 = transform_reduce(v1.begin(), v1.end(), v2.begin(), 0, plus<>(), multiplies<>()); // 二項関数オブジェクト plus, multiplies
// s1 = transform_reduce(v1.begin(), v1.end(), v2.begin(), 0, sum, mul);
// s1 = transform_reduce(v1.begin(), v1.end(), v2.begin(), 0, [](int a, int b){ return a + b; }, [](int a, int b){ return a * b; });
s2 = inner_product(v1.begin(), v1.end(), v2.begin(), 0);
printf(" 内積(transform_reduce) = %d\n", s1);
printf(" 内積(inner_product) = %d\n", s2);
return 0;
}