STL学习笔记----16.STL算法之 (数值算法)

一. 概述

用来处理数值的算法,需要加上头文件 #include<numberic>

accumulate()组合所有元素(求总和,求乘积...)
inner_product()组合两区间内的所有元素
adjacent_difference()将每个元素和其前一元素组合
partial_sum()将每个元素和其先前的所有元素组合
二. 加工运算后产生结果

1. 对序列进行某种运算

  1. //计算initValue和区间[beg, end)内所有元素的总和 
  2. //也就是: initValue + a1 + a2 + a3 +... 
  3. accumulate (InputIterator beg, InputIterator end,  
  4.             T initValue) 
  5.  
  6. //也就是:initValue op a1 op a2 ... 
  7. //比如 op 是 乘,则为:initValue * a1 * a2 * a3... 
  8. accumulate (InputIterator beg. InputIterator end,  
  9.             T initValue, BinaryFunc op) 
//计算initValue和区间[beg, end)内所有元素的总和
//也就是: initValue + a1 + a2 + a3 +...
T
accumulate (InputIterator beg, InputIterator end, 
            T initValue)

//也就是:initValue op a1 op a2 ...
//比如 op 是 乘,则为:initValue * a1 * a2 * a3...
T
accumulate (InputIterator beg. InputIterator end, 
            T initValue, BinaryFunc op)
2. 计算两序列内积

  1. //返回[beg, end)区间和beg2为起始的区间的对应元素组的内积 
  2. //initValue + (a1*b1) + (a2*b2) + (a3*b3) + ... 
  3. inner_product (InputIterator1 beg1, InputIterator1 end1,  
  4.                InputIterator2 beg2,  
  5.                T initValue) 
  6.  
  7. //initValue op1 (a1 op2 b1) op1 (a2 op2 b2) op1 ... 
  8. inner_product (InputIterator1 beg1. InputIterator1 end1,  
  9.                InputIterator2 beg2,  
  10.                T initValue,  
  11.                BinaryFunc op1, BinaryFunc op2) 
//返回[beg, end)区间和beg2为起始的区间的对应元素组的内积
//initValue + (a1*b1) + (a2*b2) + (a3*b3) + ...
T
inner_product (InputIterator1 beg1, InputIterator1 end1, 
               InputIterator2 beg2, 
               T initValue)

//initValue op1 (a1 op2 b1) op1 (a2 op2 b2) op1 ...
T
inner_product (InputIterator1 beg1. InputIterator1 end1, 
               InputIterator2 beg2, 
               T initValue, 
               BinaryFunc op1, BinaryFunc op2)
三. 相对值和绝对值之间的转换

1. 将相对值转换成绝对值

  1. //计算区间[sourceBeg, sourceEnd)中每个元素的部分和,然后写入destBeg 
  2. //a1, a1+a2, a1+a2+a3, ... 
  3. OutputIterator 
  4. partial_sum (InputIterator sourceBeg, InputIterator sourceEnd,  
  5.              OutputIterator destBeg) 
  6.  
  7. //a1, a1 op a2, a1 op a2 op a3, ... 
  8. OutputIterator 
  9. partial_sum (InputIterator sourceBeg, InputIterator sourceEnd,  
  10.              OutputIterator destBeg, BinaryFunc op) 
//计算区间[sourceBeg, sourceEnd)中每个元素的部分和,然后写入destBeg
//a1, a1+a2, a1+a2+a3, ...
OutputIterator
partial_sum (InputIterator sourceBeg, InputIterator sourceEnd, 
             OutputIterator destBeg)

//a1, a1 op a2, a1 op a2 op a3, ...
OutputIterator
partial_sum (InputIterator sourceBeg, InputIterator sourceEnd, 
             OutputIterator destBeg, BinaryFunc op)
2. 将绝对值转换成相对值

  1. //计算区间[sourceBeg, sourceEnd)中每一个元素前趋差,然后写destBeg 
  2. //a1, a2-a1, a3-a2, a4-a3, ... 
  3. OutputIterator 
  4. adjacent_difference (InputIterator sourceBeg,  
  5.                      InputIterator sourceEnd,  
  6.                      OutputIterator destBeg) 
  7.  
  8. //a1, a2 op a1, a3 op a2, a4 op a3, ... 
  9. OutputIterator 
  10. adjacent_difference (InputIterator sourceBeg,  
  11.                      InputIterator sourceEnd,  
  12.                      OutputIterator destBeg,  
  13.                      BinaryFunc op) 
//计算区间[sourceBeg, sourceEnd)中每一个元素前趋差,然后写destBeg
//a1, a2-a1, a3-a2, a4-a3, ...
OutputIterator
adjacent_difference (InputIterator sourceBeg, 
                     InputIterator sourceEnd, 
                     OutputIterator destBeg)

//a1, a2 op a1, a3 op a2, a4 op a3, ...
OutputIterator
adjacent_difference (InputIterator sourceBeg, 
                     InputIterator sourceEnd, 
                     OutputIterator destBeg, 
                     BinaryFunc op)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值