需要的头文件:
numeric

源码:

//版本1
template <class _InputIterator, class _Tp>
_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
{for ( ; __first != __last; ++__first)__init = __init + *__first;return __init;
}
//版本2
template <class _InputIterator, class _Tp, class _BinaryOperation>
_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,_BinaryOperation __binary_op)
{for ( ; __first != __last; ++__first)__init = __binary_op(__init, *__first);return __init;
}

作用:
计算init和[first,last)内所有元素的总和
通过二元仿函数我们将其一般化为:
计算init和[first,last)内所有每个元素运算值的总和。

例子:

//请使用比较完整支持c++11特性的编译器进行编译
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;int main()
{int ia[5] = {1,2,3,4,5};vector<int> iv( ia, ia+5 );//计算总和cout << accumulate( iv.begin(), iv.end(), 0 ) <<endl;//15, 0 + 1 + 2 + 3 + 4 + 5//计算2倍总和cout << accumulate( iv.begin(), iv.end(), 0,[](int init, int b){return init + 2*b;}) <<endl;//30, 0 + 1*2 + 2*2 + 3*2 + 4*2 + 5*2//计算偶数的总和cout << accumulate( iv.begin(), iv.end(), 0, [](int init, int b){if( b % 2 == 0 )return init+b;return init;} ) << endl;//6, 0 + 2 + 4//计算相乘,注意init的值需要为1cout << accumulate( iv.begin(), iv.end(), 1, [](int init, int b){return init * b;} ) <<endl;//120, 1 * 1 * 2 * 3 * 4 *5//计算方差double ave = accumulate( iv.begin(), iv.end(), 0.0 ) / iv.size();auto variance = accumulate( iv.begin(), iv.end(),0.0, [&](double init, int b){return init + (b-ave)*(b-ave);} );variance /= iv.size();cout << variance << endl;
}

注意:

  • init的类型决定了accumulate的返回类型。

  • 在使用二元仿函数时,init是第一个参数,务必注意,否则会出现一些神奇的结果

STL之accumulate相关推荐

  1. C++的STL中accumulate的用法

    accumulate定义在#include<numeric>中,作用有两个,一个是累加求和,另一个是自定义类型数据的处理 1.累加求和 int sum = accumulate(vec.b ...

  2. STL 之accumulate,adjacent_difference,inner_product,partial_sum

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

  3. C++的STL中accumulate函数用法

    accumulate(arr.begin(), arr.end(), int val); accumulate函数包含在 #include<numeric> 头文件下,其中有三个参数,前两 ...

  4. C++:STL标准入门汇总

    学无止境!!! 第一部分:(参考百度百科) 一.STL简介 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.它是由Alexander Ste ...

  5. STL中list的使用(理论)

    STL中的list就是一双向链表,可高效地进行插入删除元素.现总结一下它的操作. 文中所用到两个list对象c1,c2分别有元素c1(10,20,30) c2(40,50,60).还有一个list&l ...

  6. c语言用队列stl加头文件,C++ STL List队列用法(实例)

    #include #include #include #include using namespace std; //创建一个list容器的实例LISTINT typedef listLISTINT; ...

  7. [STL][C++]LIST

    参考:http://blog.csdn.net/whz_zb/article/details/6831817 list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素.在STL中,list ...

  8. 【STL】C++ STL超全总结

    STL目录页面 C++ STL 超全总结-基于算法竞赛(悠享版)(更好观感) 使用STL的高精度模板 vector<int> mul(vector<int> &a, v ...

  9. STL中vector、list、deque和map的区别

    vector 向量 相当于一个数组     在内存中分配一块连续的内存空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capacituy( ...

最新文章

  1. MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
  2. 用时间戳判断两个时间是否在同一天和时区转换问题
  3. ASP.NET3种验证码[转]
  4. 【MFC】vs2013_MFC使用文件之15.mfc 按钮CBitmapButton的使用
  5. 小米手环无法模拟门卡_颜值与功能得到全面升级,小米手环4 NFC版上手体验
  6. 编译C#和C++共存的解决方案的小结
  7. App Shortcuts 快捷方式 Android7 1 的3D Touch
  8. c语言坐标画图,C语言中绘图的函数库
  9. UE4--地形篇——风吹草动的草
  10. 寻找 IT 服务行业隐形冠军, 航天信息上市十年再造中国梦
  11. Python爬虫进行Web数据挖掘总结和分析
  12. 2021年中国研究生数学建模竞赛B题——空气质量预报二次建模
  13. recycleview添加item点击事件--作业三
  14. 有关队列的操作 python
  15. iOS上应用如何兼容32位系统和64位系统
  16. 游戏世界三维坐标转换为屏幕坐标原理分析:三角函数转换与矩阵变换
  17. 网络编程、正则表达式
  18. 8天3城50店,带你去广东吃遍虾饺、烧腊、猪脚饭、潮汕牛肉火锅……
  19. UCOSIII系统任务
  20. win10 删除打开方式里的多余项

热门文章

  1. lbp特征提取算法 知乎_图像-LBP特征描述算子-人脸检测
  2. cupload怎么保存图片_图片标注软件labelImg使用指南
  3. php函数clean(),wordpress函数clean_url()用法示例
  4. 不重复点名抽奖_抽奖新玩法?和平精英蜘蛛异变套装上线 参与十次可获得所有奖励...
  5. java tts引擎_Android TTS系列二——如何开发一款系统级tts引擎?
  6. jsp+java bean+mysql数据库进行分页显示
  7. 计算机英语讲课笔记01
  8. 操作技巧:在Python Shell里如何清屏
  9. 针对《关于郝培强的《为什么我们招聘的时候绝不要传智播客的学生?》》的看法
  10. php输出mysql查询结果_PHP简单获取数据库查询结果并返回JSON