zoukankan      html  css  js  c++  java
  • 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 算法介绍完成!

  • 相关阅读:
    【iOS学习笔记】iOS启动顺序
    【iOS学习笔记】iOS算法(四)之冒泡排序
    【iOS学习笔记】iOS算法(五)之折半查找
    【iOS学习笔记】iOS算法(三)之插入排序
    【iOS学习笔记】iOS算法(二)之选择排序
    【iOS学习笔记】iOS算法(一)快速排序算法
    【iOS学习笔记】iOS ⾃自定义cell的步骤
    【iOS学习笔记】IOS开发中设置applicationIconBadgeNumber和消息推送
    【iOS学习笔记】iOS 9:改用更安全的HTTPS
    2015/10/6 iOS 笔记 细节 应用中常见文件
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4021680.html
Copyright © 2011-2022 走看看