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的和。
#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的更多相关文章
- JSONObject put,accumulate,element的区别
		public Object put (Object key, Object value) 将value映射到key下.如果此JSONObject对象之前存在一个value在这个key下,当前的valu ... 
- JSONObject put,accumulate,element的区别(转载)
		原文链接:http://ljhzzyx.blog.163.com/blog/static/3838031220126810430157/ public Object put (Object key ... 
- C++ STL算法系列3---求和:accumulate
		该算法在numeric头文件中定义. 假设vec是一个int型的vector对象,下面的代码: //sum the elements in vec starting the summation wit ... 
- 转JSONObject put,accumulate,element的区别
		public Object put (Object key, Object value) 将value映射到key下.如果此JSONObject对象之前存在一个value在这个key下,当前的 ... 
- 从零开始学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 ... 
- JSONObject put accumulate element 方法区别-------java中
		1.public Object put (Object key, Object value) 将value映射到key下.如果此JSONObject对象之前存在一个value在这个key下,当前的va ... 
- C++ STD accumulate函数
		1. 介绍 用来计算特定范围内(包括连续的部分和初始值)所有元素的和,除此之外,还可以用指定的二进制操作来计算特定范围内的元素结果.其头文件在numeric中. 用次函数可以求和,构造前n项和的向量, ... 
- 【385】itertools 的 product 和 chain 和 accumulate
		参考:itertools模块 product 相当于返回两个集合中数据的所有组合可能 Examples from Eric Martin from itertools import product p ... 
- std::accumulate使用的一个小细节
		今天使用std::accumulate模板函数的时候出现了一个错误,特此记录一下. #include <iostream> #include <numeric> int mai ... 
随机推荐
- 【Maven】项目添加Maven类库依赖
			1.右击项目-->Maven-->EnableDependencyManagement,按步骤完成操作. 2.右击项目-->Properties-->DeploymentAss ... 
- BZOJ 3403: [Usaco2009 Open]Cow Line 直线上的牛( deque )
			直接用STL的的deque就好了... ---------------------------------------------------------------------- #include& ... 
- [转]apache的源码安装详细过程全纪录
			原文链接:http://www.jb51.net/article/59474.htm 文中 开机启动需要修改 而且特别麻烦 还的配置php 否则不认识php文件 郁闷!只能做参考了! 
- String "+" 的补充说明---行粒度
			String 中“+” 的操作的补充说明 在使用“+”的时候,会创建一个StringBuilder对象,然后invokevirtual append()操作 “+”操作创建StringBuilder的 ... 
- WCF Publisher/Subscriber 订阅-发布模式
			本博后续将陆续整理这些年做的一些预研demo,及一些前沿技术的研究,与大家共研技术,共同进步. 关于发布订阅有很多种实现方式,下面主要介绍WCF中的发布订阅,主要参考书籍<Programming ... 
- Windows的公共控件窗口类列表
			The following window class names are provided by the common control library: ANIMATE_CLASS Creates a ... 
- springMVC用法 以及一个简单的基于springMVC hibernate spring的配置
			替代struts 1 web.xml中配置springmvc中央控制器 <?xml version="1.0" encoding="UTF-8"?> ... 
- ASP.NET MVC 5 学习教程:生成的代码详解
			原文 ASP.NET MVC 5 学习教程:生成的代码详解 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 ... 
- perl 5.22手动安装Mysql DBI和DBD
			mysql 手动安装DBI 和DBD: DBI版本: [root@dr-mysql01 DBD-mysql-4.033]# perl -MDBI -le 'print $DBI::VERSION;' ... 
- hdu 1395 2^x mod n = 1 (简单数论)
			题目大意: 求出一个最小的x 使得 2的x次方对n取模为1 思路分析: 若要 a*b%p=1 要使得b存在 则 gcd (a,p)=1. 那么我们应用到这个题目上来. 当n为偶数 2^x 也是偶数, ... 
