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的和。

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<int> v;
const int START = 1, END = 100;
for( int i = START; i <= END; ++i )
v.push_back(i);//把1到100压入vector容器中!
int sum = accumulate( v.begin(), v.end(), 0 );//就是这个算法,非常奇妙吧。
cout << "sum from " << START << " to " << END << " is " << sum << '/n';
return 0;
}

毫无疑问,它的执行结果是"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:

#include <iostream>
#include <vector>
#include <string>
#include <numeric>
using namespace std;
int main ()
{
string str = "Hello World!";
vector<string> vec(10,str); // vec = ["Hello World!", "Hello World!", ...]
string a = accumulate( vec.begin(), vec.end(), string("HaHaHa----") );
cout << a << endl; // displays "HaHaHa----Hello World!Hello World!Hello..."
return 0;
}

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

accumulate的更多相关文章

  1. JSONObject put,accumulate,element的区别

    public Object put (Object key, Object value) 将value映射到key下.如果此JSONObject对象之前存在一个value在这个key下,当前的valu ...

  2. JSONObject put,accumulate,element的区别(转载)

    原文链接:http://ljhzzyx.blog.163.com/blog/static/3838031220126810430157/   public Object put (Object key ...

  3. C++ STL算法系列3---求和:accumulate

    该算法在numeric头文件中定义. 假设vec是一个int型的vector对象,下面的代码: //sum the elements in vec starting the summation wit ...

  4. 转JSONObject put,accumulate,element的区别

        public Object put (Object key, Object value) 将value映射到key下.如果此JSONObject对象之前存在一个value在这个key下,当前的 ...

  5. 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)

    一.移除性算法 (remove)  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  6. JSONObject put accumulate element 方法区别-------java中

    1.public Object put (Object key, Object value) 将value映射到key下.如果此JSONObject对象之前存在一个value在这个key下,当前的va ...

  7. C++ STD accumulate函数

    1. 介绍 用来计算特定范围内(包括连续的部分和初始值)所有元素的和,除此之外,还可以用指定的二进制操作来计算特定范围内的元素结果.其头文件在numeric中. 用次函数可以求和,构造前n项和的向量, ...

  8. 【385】itertools 的 product 和 chain 和 accumulate

    参考:itertools模块 product 相当于返回两个集合中数据的所有组合可能 Examples from Eric Martin from itertools import product p ...

  9. std::accumulate使用的一个小细节

    今天使用std::accumulate模板函数的时候出现了一个错误,特此记录一下. #include <iostream> #include <numeric> int mai ...

随机推荐

  1. libev源代码浅析

    libev是一个开源的事件驱动库,基于epoll,kqueue等OS提供的基础设施.其以高效出名,它可以将IO事件,定时器,和信号统一起来,统一放在事件处理这一套框架下处理. libev的基本使用方法 ...

  2. application(expand)--easyui

    一,效果图. 二,源代码. <!DOCTYPE html><html><head> <meta charset="UTF-8"> & ...

  3. Android:OptionMenu

    MainActivity: package com.example.optionmenu; import android.content.Intent; import android.os.Bundl ...

  4. 窗体区域绘制问题WS_CLIPCHILDREN与WS_CLIPSIBLINGS

    WS_CLIPCHILDREN,使得父窗体在绘制时留出其上的子窗体的位置不去画它,而那片区域留着子窗体去画.WS_CLIPSIBLINGS,必须用于子窗体,使得该子窗体在收到WM_PAINT时同时令其 ...

  5. Hadoop实战实例

    Hadoop实战实例        Hadoop实战实例        Hadoop 是Google MapReduce的一个Java实现.MapReduce是一种简化的分布式编程模式,让程序自动分布 ...

  6. 设计模式(八)装饰器模式Decorator(结构型)

    设计模式(八)装饰器模式Decorator(结构型) 1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法 ...

  7. Android Activity Fragment 生命周期

    从开源项目中看到 这个,就情不自禁的收藏了~ https://github.com/xxv/android-lifecycle

  8. jstl 保留两位小数

    简介:jstl保留两位小数实现方法在JSP开头导入以下标签:<%@tagliburi="http://java.sun.com/jsp/jstl/fmt"pre jstl 保 ...

  9. debian安装jdk8

    1.官方下载java想安装的JAVA版本: http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.比如说,常安装的J ...

  10. MySQL如何查询LINESTRING数据

    我有一个提交的命名crm_geo_org,具有以下结构 ipoid INTEGER 11 NOT NULL PRIMARY KEY beginip INTEGER 14 NOT NULL UNSIGN ...