C++ accumulate()函数的用法
accumulate定义在 numeric 中,作用有两个,一个是累加求和,另一个是自定义类型数据的处理。
头文件
#include <numeric>
原型
默认求累加和
template <class InputIterator, class T>
T accumulate (InputIterator first, InputIterator last, T init)
{
while (first!=last) {
init = init + *first; // or: init=binary_op(init,*first) for the binary_op version
++first;
}
return init;
}
自定义数据的处理
template <class InputIterator, class T, class BinaryOperation>
T accumulate (InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
参数
- first,last:迭代器,指向要操作的序列的区间[first, last)。first指向的元素参与计算,last不参与计算;
- init:初始值,如在init的基础上进行求和计算;
- binary_op:Binary operation taking an element of type T as first argument and an element in the range as second, and which returns a value that can be assigned to type T. This can either be a function pointer or a function object. The operation shall not modify the elements passed as its arguments.
案例
// accumulate example
#include <iostream> // std::cout
#include <functional> // std::minus
#include <numeric> // std::accumulate
int myfunction (int x, int y) {return x+2*y;}
struct myclass {
int operator()(int x, int y) {return x+3*y;}
} myobject;
int main () {
int init = 100;
int numbers[] = {10,20,30};
std::cout << "using default accumulate: ";
std::cout << std::accumulate(numbers,numbers+3,init);
std::cout << '\n';
std::cout << "using functional's minus: ";
std::cout << std::accumulate (numbers, numbers+3, init, std::minus<int>());
std::cout << '\n';
std::cout << "using custom function: ";
std::cout << std::accumulate (numbers, numbers+3, init, myfunction);
std::cout << '\n';
std::cout << "using custom object: ";
std::cout << std::accumulate (numbers, numbers+3, init, myobject);
std::cout << '\n';
return 0;
}
输出:
using default accumulate: 160
using functional's minus: 40
using custom function: 220
using custom object: 280
C++ accumulate()函数的用法的更多相关文章
- 有关日期的函数操作用法总结,to_date(),trunc(),add_months();
相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...
- Oracle to_date()函数的用法
Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...
- js中bind、call、apply函数的用法
最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...
- Oracle trunc()函数的用法
Oracle trunc()函数的用法 /**************日期********************/1.select trunc(sysdate) from dual --2013-0 ...
- freemarker内置函数和用法
原文链接:http://www.iteye.com/topic/908500 在我们应用Freemarker 过程中,经常会操作例如字符串,数字,集合等,却不清楚Freemrker 有没有类似于Jav ...
- matlab中patch函数的用法
http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...
- JavaScript中常见的数组操作函数及用法
JavaScript中常见的数组操作函数及用法 昨天写了个帖子,汇总了下常见的JavaScript中的字符串操作函数及用法.今天正好有时间,也去把JavaScript中常见的数组操作函数及用法总结一下 ...
- JavaScript中常见的字符串操作函数及用法
JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
- oracle的substr函数的用法
oracle的substr函数的用法 取得字符串中指定起始位置和长度的字符串 substr( string, start_position, [ length ] ) 如: substr( ...
- java String.split()函数的用法分析
java String.split()函数的用法分析 栏目:Java基础 作者:admin 日期:2015-04-06 评论:0 点击: 3,195 次 在java.lang包中有String.spl ...
随机推荐
- pg的json类型
以下举例说明: postgres=# select '{"b":1,"a":2}'::json; json --------------- {"b&q ...
- 逆向学习物联网-网关ESP8266-04系统联合调试
1.测试平台原理 2.搭建硬件测试平台 3.软件测试平台 1)串口终端 2)串口监视 3)OneNET后台服务 https://open.iot.10086.cn/passport/login/ 户名 ...
- [478] C2 Age Of Splendor Opcodz
[478] C2 Age Of Splendor Client 00 SendProtocolVersion 01 MoveBackwardToLocation 02 Say 03 RequestEn ...
- sourceTree工具使用方法
https://www.cnblogs.com/tian-xie/p/6264104.html
- app内打开外部第三方瞎下载链接
Q:我用cordova开发项目,想在app内跳转外部链接,安装了cordova-plugin-inappbrowser后确实可以跳转,但是跳转的页面有个按钮,原本点击会下载app,现在点击后毫无反应, ...
- FATAL Exited too quickly (process log may have details)的解决方案
作为一个混混的开发,不会啥容器操作.所以一般都是用supervisor来管理一些运行的进程 用了一段时间还是比较好用的,这个软件也是用的Python开发. 但在使用的过程中,status时会出现 FA ...
- jetbrain 全套激活
关于 jetbrain 专业版激活的教程很多,发现很多实际操作不太友好,本人亲测可激活经理 1.下载 ja-ja-netfilter-all https://github.com/byebai95/j ...
- 20200922--计算矩阵边缘元素之和(奥赛一本通P91 3二维数组)
输入一个整数矩阵,计算位于矩阵边缘的元素之和.所谓矩阵边缘的元素,就是就一行和最后一行以及第一列和最后一列的元素. 输入; 第一行分别为矩阵的行数m和列数n(m<100,n<100),两者 ...
- char和int的类型转换
char类型是16位的,底层采用unicode编码保存.char类型是可以直接赋值给int类型的,因为是16位到32位低到高.举个例子比如int i='1';打印i的值是49.char类型跟int类型 ...
- 3月1日Android开学学习
Android开发的简单控件 1.文本显示 2.视图基础 3.常用布局 4.按钮触控 5.图像显示 文本显示 (1)设置文本内容 1.在XML文件中通过属性Android:text设置文本 Andro ...