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 ...
随机推荐
- 【Eclipse】修改项目访问名称
Properties --> Web Project Settings --> Context root --> 输入想要用的名称(默认是项目名)
- activity_main.xml
activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- 面向对象程序设计-C++ Type conversion (Static) & Inheritance & Composition【第十二次上课笔记】
这节课继续讲解了 static 作为静态数据成员 / 成员函数的用法 具体详解我都已注释出来了,大家可以慢慢看 有任何问题都可以在这篇文章下留言我会及时解答 :) //static 静态数据成员 // ...
- beep的控制程序
参照艾米电子的程序进行改写的 //date : 2014,5,4 module for_beep ( clock , reset , out_beep ); input clock ,reset ; ...
- ORACLE存储过程笔记2
ORACLE存储过程笔记2 运算符和表达式 关系运算 =等于<>,!=不等于<小于>大于<=小于等于>=大于等于 一般运算 +加-减*乘/除 ...
- 设计模式 ( 二十 ) 访问者模式Visitor(对象行为型)
设计模式 ( 二十 ) 访问者模式Visitor(对象行为型) 1.概述 在软件开发过程中,对于系统中的某些对象,它们存储在同一个集合collection中,且具有不同的类型,而且对于该集合中的对象, ...
- 利用cmake来搭建开发环境
对于经常在终端下写程序的non-windows程序员,Makefile绝对是最常用的工具,小到一个文件的简单的测试程序,大到数百个文件的商业软件,只需要有shell,一个make命令就可得到可运行的程 ...
- Activity的创建和使用
Activity: 1:创建一个类继承Activity或者它的子类 public class MainActivity extends Activity { @Override protected v ...
- 10个SQL注入工具(转载)
众所周知,SQL注入攻击是最为常见的Web应用程序攻击技术.同时SQL注入攻击所带来的安全破坏也是不可弥补的.以下罗列的10款SQL注入工具可帮助管理员及时检测存在的漏洞. BSQL Hacker 1 ...
- javascript每日一练(十三)——运动实例
一.图片放大缩小 <!doctype html> <html> <head> <meta charset="utf-8"> < ...