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 ...
随机推荐
- 简单的js hook
// ==UserScript== // @name ethereum request // @namespace http://tampermonkey.net/ // @version 0.1 / ...
- leetcode 27. 移除元素 【时间击败100.00%】【内存击败84.67%】
1 public int removeElement(int[] nums, int val) { 2 int last = nums.length - 1; 3 for (int i = 0; i ...
- maven概述,maven依赖管理的概念,maven一键构建概念
maven概述 Maven在美国是一个口语化的词语,代表专家,内行的意思 一个对Maven比较正式的定义是这么说的,Maven是一个项目管理工具,它包含了一个项目对象模型(POM:Project O ...
- JavaSE——面向对象(类与对象)
package com.zhao.test1; public class GirlFriend { //属性 String name; int age; String gender; //行为 pub ...
- python 实现两个多维数组去重处理
a = [[1,2,3,4],[5,6,7,8],[11,12,13,14],[23,24,12,11]] b = [[5,6,7,8],[23,24,12,11]] c = [] for i in ...
- 51定时器:0xee的由来
定时时间=(65536-初值)×(12/晶振频率) 因为51实验板的晶振为11.0592,所以 定时时间=(65536-初值)×(12/11.0592) 定时1ms:1000=(65536-初值)×( ...
- 射频识别技术(RFID)
概述: 无线射频识别即射频识别技术(Radio Frequency Identification,RFID),是自动识别技术的一种,通过无线射频方式进行非接触双向数据通信,利用无线射频方式对记录媒体( ...
- shell语法2-默认变量、数组
一:文件参数变量 1.在执行shell脚本时,可以向脚本传递参数.$1是第一个参数,$2是第二个参数,以此类推.特殊的,$0是文件名(包含路径) #! /bin/bashecho "文件名: ...
- setAttribute动态添加
btnDel.setAttribute('type', 'button'); //type="button" btnDel.setAttribute('value', '删除'); ...
- 移动APP测试要点
一. UI测试 (1) 页面布局 ① 页面布局合理且友好,符合用户习惯 ② 列表型界面有滚动条 ③ 功能入口明显,容易找到 (2) 图形测试 ① 图片大小合适且清晰 ② 页面字体与风格一致 ③ 背景颜 ...