accumulate

accumulate

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

accumulate?就是sum up a range of elements。呵呵。这个挺简单的。以下是这个算法的简单介绍:

Syntax:
#include <numeric>//呵呵,使用这个算法这个头文件是必需要包含进来滴!
TYPE accumulate( input_iterator start, input_iterator end, TYPE val );
TYPE accumulate( input_iterator start, input_iterator end, TYPE val, BinaryFunction f );

The accumulate function computes the sum of val and all of the elements in the range [start,end).
If the binary function f is specified, it is used instead of the + operator to perform the summation.
The accumulate function runs in linear time. //我看了非常久,linear time 应该说的这个算法的复杂度是O(n)吧,呵呵~~~百度之貌似没有结果。

 

嗯,废话少说,以下来看它的应用。用这个算法来计算1到100的和。

毫无疑问,它的执行结果是”sum from 1 to 100 is 5050″值得注意的是,TYPE accumulate( input_iterator start, input_iterator end, TYPE val );val也是要加进去滴!上面是0,肯定等于没加!

当然,这个程序我们一般用个for循环解决就是,干嘛还要这么大费周折呢。呵呵,事实上 accumulate 可爱的地方不只在于对于数字运算支持,对于非数值运算也是支持的!

The accumulate function can also be used on non-numerical types. The following example uses accumulate to concatenate all of the strings in a vector into a single string:

 

 

呵呵,第一个STL C++ Algorithms accumulate 算法介绍完成!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118292.html原文链接:https://javaforall.net

(0)
上一篇 2021年12月8日 上午9:00
下一篇 2021年12月8日 上午9:00


相关推荐

  • python数组基本操作总结

    python数组基本操作总结一 列表 元祖 nbsp nbsp 1 元祖 nbsp nbsp nbsp nbsp nbsp nbsp 1 创建 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp tuple01 创建空元组 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp tuple01 2 nbsp 元组中只包含一个元素时 需要在元素后面添加逗号 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp tuple01 joe susan black monika nbsp nbsp nbsp nbsp nbsp nbsp 2 将元组转换为

    2026年3月19日
    2
  • 数十款代码编辑器,PyCharm为何如此优秀?

    数十款代码编辑器,PyCharm为何如此优秀?这两年被 Python 初学小白问到最多的问题就是 该用什么代码编辑工具 说实话 我个人是用 JupyterNoteb 最多 主要是经常做数据可视化 方便些 但对于初学者来说 PyCharm 仍是不二的选择 甚至我建议你只用 PyCharm 从当前所有主流 PythonIDE 来看 PyCharm 是最适合做 Python 开发的 特别对新手而言 可以节省很多不必要的时间成本 我也常用 PyCharm 写脚本 偶尔开发一些 web 应用 PyCharm 的完善程度可以说没有一个 IDE 能达到 其他 IDE 相比 PyCha

    2026年3月27日
    1
  • OpenProcessToken令牌函数使用方法

    OpenProcessToken令牌函数使用方法转载自:https://www.cnblogs.com/mengfanrong/p/5185850.html GetCurrentProcessID得到当前进程的IDOpenProcessToken得到进程的令牌句柄LookupPrivilegeValue查询进程的权限AdjustTokenPrivileges调整令牌权限要对一个随意进程(包括系统安全进程和服务进程)进行指定…

    2022年6月25日
    24
  • debounce与throttle区别

    debounce与throttle区别在2011年,Twitter网站曾爆出一个问题:在主页往下滚动时,页面会变得缓慢以致没有响应。JohnResig发表了一篇文章《ablogpostabouttheproblem》指出直接在scroll事件上面绑定高消耗的事件是一个多么愚蠢的想法。现在项目中大家都会对类似的scroll或者resize事件都进行了节流控制,下述是我们经常用到,也是《JavaScript高级程序设计》-J

    2022年6月20日
    24
  • ASP.NET Jumpstart:Media Share Library Starter Kit 简介

    ASP.NET Jumpstart:Media Share Library Starter Kit 简介

    2022年3月12日
    37
  • 过滤数组中的空数组array_filter()

    过滤数组中的空数组array_filter()

    2021年11月8日
    53

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号