std :: accumulate

累计范围内的值
返回将范围中的所有值累加[first,last)到init的结果。

默认操作是向上添加,但可以将不同的操作指定为binary_op。

accumulate (InputIterator first, InputIterator last, T init, BinaryOperation binary_op);

参数
first,last:
将迭代器输入到序列中的初始位置和最终位置。使用的范围是[first,last),它包含所有的元件第一和最后一个,包括由指向的元件第一但不被指向的元素最后。

init
累加器的初始值。

binary_op
myfunction (int x, int y);这样的函数时,init传入x,前面的范围和传入y,最后返回函数值。
std::minus();返回init-范围和
int operator()(int x, int y);和函数那个一样效果。

返回值
累积init:和范围内所有元素的结果[first,last)。

// accumulate example

#include <iostream>     // std::cout
#include <functional>   // std::minus
#include <numeric>      // std::accumulateint 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;
}

编辑并运行

输出:

使用默认累积:160
使用函数的减号:40
使用自定义功能:220
使用自定义对象:280

std::accumulate的用法相关推荐

  1. std::accumulate的具体用法

    文章目录 std :: accumulate 头文件 原型 参数 返回值 用例1 用例2:Lambda表达式 std :: accumulate 作用累加求和,对于字符串可以将其连接起来(string ...

  2. 【KATA练习日记】关于std::accumulate的使用

    前言 今天的练习是6段难度的题目-> Find the odd int | Codewars Given an array of integers, find the one that appe ...

  3. C++11 std::bind std::function 高级用法

    C++11 std::bind std::function 高级用法 (c++11的新特性) 原文:https://blog.csdn.net/yangjie6898862/article/detai ...

  4. C++11标准模板(STL)- 算法 - 数值运算 (std::accumulate)

    定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...

  5. C++ std::accumulate

    C++中的accumulate是一个模板函数,要使用这个函数需要包含numeric这个头文件. #include <iostream> #include <numeric> # ...

  6. std::accumulate

    std::accumulate 头文件 #include <numeric> 作用累加求和,对于字符串可以将其连接起来(string类型的加,相当于字符串连接) 例:累加求和 std::v ...

  7. C++中std::string::find_last_of用法

    早上本来还看代码时本来还迷迷糊糊,结果被find_last_of一下给震醒了,std::string 使用了这么多年,今天才发现find_last_of只是用来查找字符的,多年来一直以为它可用来查找字 ...

  8. boost::fibers::buffered_channel< std::string >用法的测试程序

    boost::fibers::buffered_channel< std::string >用法的测试程序 实现功能 C++实现代码 实现功能 boost::fibers::buffere ...

  9. C++for_each| bind1st | ptr_fun | std::function的用法

    c++ for_each 用法_小键233-CSDN博客 传入参数 要传入参数给global function ,需要使用 ptr_fun() 这个 function adapter 将global ...

最新文章

  1. Vant 使用之Toast Vant安装和使用
  2. Point-to Analysis指针分析(2)
  3. 一个html的文件当中读取另一个html文件
  4. windows winrar 指令_【转】winrar命令行详解
  5. Mysql数据库按时间点恢复实战
  6. MFC C++ Cstring与string互转
  7. Android Studio 创建第一个项目应用
  8. SQL SERVER中的纵横查询
  9. 海康监控摄像头能做视频直播那些事!
  10. python学习笔记(八)传递任意数量的实参
  11. 免费好用的英语单词统计软件(带翻译功能)
  12. win10的右键压缩菜单
  13. 甘思咪哚,肉骨茶,Greenland
  14. Zigbee系列 学习笔记五(信道选择)
  15. google的新闻(文章)分类算法
  16. VB.net小技巧——使用DataGridView显示EXECL表格内容
  17. [硬件选型] 光源及照明方式的选择
  18. C++ has-a关系之包含
  19. SEO新手站长说说之网站更新频率多少才合适
  20. Windows中安全弹出U盘的操作__2018.06.02

热门文章

  1. 江苏省对口单招分数线计算机,2021年江苏省对口单招分数线公布 江苏省对口单招省控线出炉...
  2. win策略计算机配置,BGinfo配置策略(Server2012R2-Win10)
  3. 酒水饮料新消费领域商城,一键下单轻松享受足不出户的快乐
  4. apt dpkg 错误制造
  5. 处理超时订单(超时未付款)的解决方案
  6. 下单以及订单处理流程描述
  7. PEPL命令-交互式解释器
  8. 实现多个文件夹名同时重命名的操作
  9. How to use Hibernate - XML Configuration
  10. 【区块链108将】微数链林道坤:区块链有助于更好的发挥大数据价值