#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
int add(int a,int b);
int cube( int a , int b );
int square(int a,int b);
int main()
{
int a[] = { 1,2,3,4,5 };
vector < int > v( a, a + sizeof(a)/sizeof(*a) );
cout << "The elements' sum is:"<<accumulate(v.begin(),v.end(),0,add)<<endl
<<"The elements' square sum is:"<<accumulate(v.begin(),v.end(),0,square)<<endl
<< "The elements' cube sum is:"<<accumulate( v.begin(), v.end(), 0, cube)<<endl;
cout<<inner_product(v.begin(),v.end(),v.begin(),10)<<endl;
//10+1*1+2*2+3*3+4*4+5*5
cout<<inner_product(v.begin(),v.end(),v.begin(),10,minus<int>(),plus<int>())<<endl;
//10 - 1+1 - 2+2 - 3+3 - 4+4 - 5+5
//将迭代器绑定在cout,作为输出
ostream_iterator<int> oite(cout," ");
partial_sum(v.begin(),v.end(),oite);
cout<<endl;
//1 3 6 10 15,#n的值等于前n个数的相加结果
partial_sum(v.begin(),v.end(),oite,minus<int>());
cout<<endl;
//1 -1 -4 -8 -13,#n的值等于前n个数的运算结果
adjacent_difference(v.begin(),v.end(),oite,plus<int>());
cout<<endl;
//1 3 5 7 9,#1照写,#n的值等于op(#n,#n-1)
adjacent_difference(v.begin(),v.end(),oite);
cout<<endl;
//1 1 1 1 1,#1照写,#n的值等于(#n - #n-1)
return 0;
}
int add(int a,int b)
{
return a + b;
}
int cube( int a,int b )
{
return a + b*b*b;
}
int square(int a,int b)
{
return a+b*b;
}