accumulate
咱们来介绍一下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相关推荐
- Pyhton 内置 itertools 模块chain、accumulate、compress、drop、take等函数使用
Python 内置的 itertools 模块使用了 yield 生成器. 1. chain 拼接迭代器 chain 函数实现元素拼接,原型如下,参数 * 表示可变的参数: chain(*iterab ...
- C++:uniform_int_distribution、rand()和mt19937随机数、partial_sum、容器最小值和最大值、accumulate
C++:uniform_int_distribution.rand()和mt19937随机数.partial_sum.容器最小值和最大值.accumulate 1.uniform_int_distri ...
- 累加求和 Accumulate.java
//1+2+3+4+......100=? public class Accumulate { //累加求和 public static void main(String[] args){ int s ...
- C++ STL算法之accumulate函数
1. 介绍 用来计算特定范围内(包括连续的部分和初始值)所有元素的和,除此之外,还可以用指定的二进制操作来计算特定范围内的元素结果.其头文件在numeric中. accumulate原函数声明定义如 ...
- STL 中的 accumulate(C++)
STL中的accumulate的头文件为#include<numeric>, accumulate有三个形参:前两个形参是要累加的元素范围,第三个是累加的初值 数组求和 vector< ...
- STL 之accumulate,adjacent_difference,inner_product,partial_sum
accumulate,adjacent_difference,inner_product,partial_sum 这些算法都是数字算法,因此只能操作数字类型的数据. 头文件 #include < ...
- 从零开始学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 ...
- vector的求和用法accumulate
一:直接上码演示 #include<bits/stdc++.h> using namespace std;int main(){//vector的初始化 vector<int> ...
- STL源码剖析 数值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa
//版本1 template <class InputIterator,class T> T accumulate(InputIterator first,InputIterator la ...
- accumulate返回值类型 提醒
一个简单的序列求和算法,用accumulate还必须传第三个参数. accumulate(VecCurrentTerse.begin(), VecCurrentTerse.end(), 0.0); 注 ...
最新文章
- 基于IntelVt技术的Linux内核调试器 - 2
- pku 1486 求出二分匹配图中的必须边
- PAT甲级1124 Raffle for Weibo Followers :[C++题解]哈希表、微博转发抽奖
- AngularJS:在Windows上安装Yeoman
- javase中的super、this和protected关键字
- 数据库添加一个列的唯一约束
- 微软“作死”Windows
- Android基础之Intent的几种常用方法
- 非对称加密提交表单到PHP
- 随笔之生成简单的验证码
- 网易评论真搞笑~~~呵呵
- realsenseD400系列使用realense-ros出现“symbol lookup error ... undefined symbol: _ZN2cv3M “解决方法(这个问题在我这里终结)
- MyBatis注解开发方式
- 一般信道容量的计算matlab,基于Matlab的信道容量计算
- Vue 3 模板语法
- micropython stm32f107_stm32f107 USART3数据接收错误问题
- VUE使用JS-SDK实现微信分享好友功能(通过点击控件触发)
- android 在状态栏耳机图标显示图标显示图标显示图标,Android 通知栏图标
- 弱网测试之NEWT(Network Emulator Toolkit)
- AI视频换脸方向论文阅读
热门文章
- 需求方案撰写之售前方案
- 1.6 回归评估准确性的指标
- 优化理论13----黄金分割法、单峰函数、python实现及验证
- 28线性空间02——坐标、坐标变换与基变换、过度矩阵
- 在机器学习、大数据等领域工作,该学Hadoop还是Spark?
- 深度学习:卷积神经网络CNN
- 计算机专业英语课后答案北京理工大学,计算机专业英语
- python axis 1_Python之NumPy(axis=0 与axis=1)区分
- html插入mv,视屏如何插入html
- php中表单名称未定义,php – zf2,表单集合没有在zf2中创建正确的输入名称