数值算法

#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;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值