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 ...
随机推荐
- iTextSharp.text的一个使用,主要用来创建PDF
using iTextSharp.text; //创建一个字体来使用和编码格式 BaseFont baseFont = BaseFont.CreateFont("C:\\Windows\\F ...
- CentOS服务器下对mysql的优化
原文链接: CentOS服务器下对mysql的优化 一.mysql的优化思路 mysql的优化分为两方面: 1. 服务器使用前的优化 2. 服务使用中的优化 二.mysql的基础优化步骤 1. 硬件级 ...
- vhost文件设置
#例子<VirtualHost *.82> #设置端口号为82 ServerName localhost #服务器名称 DocumentRoot "d:/Web" #文 ...
- 利用Tomcat的用户名和密码构建“永久”后门
从本案例中可以学到: (1)关于JSP的一些基础知识 (2)利用Tomcat的用户名和密码来构建后门 本文首先要感谢<黑客手册>“非安全.后生”编辑为本为提供了帮助,还要感谢网友“伤心的鱼 ...
- IEEE浮点数float、double的存储结构
众所周知,C的float.VB的Single都是32位浮点数变量类型(也叫单精度浮点数),C的double和VB的Double则都是64位的浮点数变量类型(也叫双精度浮点数).有些编译器还支持更屌的l ...
- JAVAC 命令使用方法
结构 javac [ options ] [ sourcefiles ] [ @files ] 參数可按随意次序排列. options 命令行选项. sourcefiles 一个或多个要编译的源文件( ...
- iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前
对于不同的苹果设备.检查每个参数<iOS:机型參数.sdk.xcode各版本号>. 机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了.表示机器屏幕尺寸变大了: 像素:表示屏幕 ...
- 设置不输入密码ssh登录
在/etc/hosts文件下加入: 192.168.1.60 u60 #设置u60为主机名 在每个节点上创建RSA秘钥: # ssh-keygen -t rsa # 一直按确定键即可 # touch ...
- CentOS安装postgresql 9.4
第一步:在CentOS6.5下安装Postgresql 1. 安装PostgreSQL源 # yum install http://yum.postgresql.org/9.4/redhat/rhel ...
- Nginx的500,502,504错误解决方法
Nginx的500,502,504错误解决方法 一.解决500错误: 1.500错误指的是服务器内部错误,也就是服务器遇到意外情况,而无法履行请求. 2.500错误一般有几种情况: (1)web脚本错 ...