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 ...
随机推荐
- 设计模式 - 单例模式mysql数据库操作类
待续... index.php 调用方法: <?php header('Content-Type:text/html; charset=utf8'); require 'instance.php ...
- HDU2527:Safe Or Unsafe(哈弗曼树)
Problem Description Javac++ 一天在看计算机的书籍的时候,看到了一个有趣的东西!每一串字符都可以被编码成一些数字来储存信息,但是不同的编码方式得到的储存空间是不一样的!并且当 ...
- WEB开发:如何用js来模拟服务器的ajax响应,不依赖服务器来编写前端代码
一.问题的提出 目前web前端开发,主流的思路是: 1)编写静态的html文件(不使用模板技术,与服务器无关) 2)页面通过ajax与服务器交互,进行数据的传输,数据格式为json格式 这里存在一个问 ...
- 使用adb签名并安装Android程序
首先需要准备Android SDK包,我是在windows上操作的,在PATH中配置 YOUT_SDK_PATH\android-sdk-windows\platform-tools 和 YOUT ...
- 重装系统后搭建php环境
重装系统后,不需要重新下载php,apache,mysql的程序包,只需要在命令行重新安装即可. 1.安装apache: 打开命令行 cd 程序目录\bin httpd -k install 按ent ...
- leetcode第一刷_Binary Tree Inorder Traversal
递归实现当然太简单,也用不着为了ac走这样的捷径吧..非递归实现还挺有意思的. 树的非递归遍历一定要借助栈,相当于把原来编译器做的事情显式的写出来.对于中序遍历,先要訪问最左下的节点,一定是进入循环后 ...
- [leetcode] Reverse Linked List 分类: leetcode 算法 2015-07-09 18:44 2人阅读 评论(0) 收藏
反转链表:比较简单的问题,可以遍历也可以递归. # Definition for singly-linked list. class ListNode: def __init__(self, x): ...
- c# 阶段总结
然并卵然并卵然并卵然并卵然并卵然并卵然并卵
- c语言统计字符数(判断a-z哪个字符出现次数最多)
http://poj.grids.cn/practice/2742 描述判断一个由a-z这26个字符组成的字符串中哪个字符出现的次数最多输入第1行是测试数据的组数n,每组测试数据占1行,是一个由a-z ...
- UIView详解1
一个UIView的实例就是一个视图,表示的是屏幕上的一块矩形区域,负责这块矩形区域的描绘以及和用户的交互. 第一.UIView的可视化属性 1. backgroundColor 背景属性 2. hi ...