咱们来介绍一下STL里的一个功能强大的函数:accumulate。意思是自动加和,所以听这个名字就会感觉它能代替许多for或者递归来加和的代码,下面来看它的实现:

版本1:

template <class InputIterator,class T>
T accumulate(InputIterator first,InputIterator last,T init,BinaryOperation binart_op)
{for( ;first != last; ++first)init = briary_op(init,*first);//对每个元素执行二元操作return init;
}

这种版本比较复杂也不好理解,本人也不常用这种,咱来看看版本2:

template <class InputIterator,class T>
T accumulate(InputIterator first,InputIterator last,T init)
{for( ; first != last; first++)init = init + *first;//将每个元素值累加到初值上return init;
}

  

  算法accumulate用来计算init和[first,last)内所有元素之和。注意,你一定得提供一个初值init,这么做的原因之一是当[first,last)为空时仍能获得一个明确定义的值。如果希望计算[first,last)中所有数值的总和,应将init设为0。

  

  式中的二元操作符不必满足交换律和结合律。是的,accumulate的行为顺序有明确定义:先将init初始化,然后针对[first,last)区间中的每一个迭代器i,依序执行init = init + *i(版本2)或init = binary_op(init, *i)(版本2)。

转载于:https://www.cnblogs.com/Zhoier-Zxy/p/8319849.html

accumulate相关推荐

  1. Pyhton 内置 itertools 模块chain、accumulate、compress、drop、take等函数使用

    Python 内置的 itertools 模块使用了 yield 生成器. 1. chain 拼接迭代器 chain 函数实现元素拼接,原型如下,参数 * 表示可变的参数: chain(*iterab ...

  2. C++:uniform_int_distribution、rand()和mt19937随机数、partial_sum、容器最小值和最大值、accumulate

    C++:uniform_int_distribution.rand()和mt19937随机数.partial_sum.容器最小值和最大值.accumulate 1.uniform_int_distri ...

  3. 累加求和 Accumulate.java

    //1+2+3+4+......100=? public class Accumulate { //累加求和 public static void main(String[] args){ int s ...

  4. C++ STL算法之accumulate函数

    1. 介绍 用来计算特定范围内(包括连续的部分和初始值)所有元素的和,除此之外,还可以用指定的二进制操作来计算特定范围内的元素结果.其头文件在numeric中.  accumulate原函数声明定义如 ...

  5. STL 中的 accumulate(C++)

    STL中的accumulate的头文件为#include<numeric>, accumulate有三个形参:前两个形参是要累加的元素范围,第三个是累加的初值 数组求和 vector< ...

  6. STL 之accumulate,adjacent_difference,inner_product,partial_sum

    accumulate,adjacent_difference,inner_product,partial_sum 这些算法都是数字算法,因此只能操作数字类型的数据. 头文件 #include < ...

  7. 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)...

    一.移除性算法 (remove) C++ Code  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 26 27 2 ...

  8. vector的求和用法accumulate

    一:直接上码演示 #include<bits/stdc++.h> using namespace std;int main(){//vector的初始化 vector<int> ...

  9. STL源码剖析 数值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa

    //版本1 template <class InputIterator,class T> T accumulate(InputIterator first,InputIterator la ...

  10. accumulate返回值类型 提醒

    一个简单的序列求和算法,用accumulate还必须传第三个参数. accumulate(VecCurrentTerse.begin(), VecCurrentTerse.end(), 0.0); 注 ...

最新文章

  1. 基于IntelVt技术的Linux内核调试器 - 2
  2. pku 1486 求出二分匹配图中的必须边
  3. PAT甲级1124 Raffle for Weibo Followers :[C++题解]哈希表、微博转发抽奖
  4. AngularJS:在Windows上安装Yeoman
  5. javase中的super、this和protected关键字
  6. 数据库添加一个列的唯一约束
  7. 微软“作死”Windows
  8. Android基础之Intent的几种常用方法
  9. 非对称加密提交表单到PHP
  10. 随笔之生成简单的验证码
  11. 网易评论真搞笑~~~呵呵
  12. realsenseD400系列使用realense-ros出现“symbol lookup error ... undefined symbol: _ZN2cv3M “解决方法(这个问题在我这里终结)
  13. MyBatis注解开发方式
  14. 一般信道容量的计算matlab,基于Matlab的信道容量计算
  15. Vue 3 模板语法
  16. micropython stm32f107_stm32f107 USART3数据接收错误问题
  17. VUE使用JS-SDK实现微信分享好友功能(通过点击控件触发)
  18. android 在状态栏耳机图标显示图标显示图标显示图标,Android 通知栏图标
  19. 弱网测试之NEWT(Network Emulator Toolkit)
  20. AI视频换脸方向论文阅读

热门文章

  1. 需求方案撰写之售前方案
  2. 1.6 回归评估准确性的指标
  3. 优化理论13----黄金分割法、单峰函数、python实现及验证
  4. 28线性空间02——坐标、坐标变换与基变换、过度矩阵
  5. 在机器学习、大数据等领域工作,该学Hadoop还是Spark?
  6. 深度学习:卷积神经网络CNN
  7. 计算机专业英语课后答案北京理工大学,计算机专业英语
  8. python axis 1_Python之NumPy(axis=0 与axis=1)区分
  9. html插入mv,视屏如何插入html
  10. php中表单名称未定义,php – zf2,表单集合没有在zf2中创建正确的输入名称