📄 acc_0611.htm
字号:
<HTML><TITLE>accumulate</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>accumulate</H2>
<HR><PRE> Generalized Numeric Operation</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Accumulate all elements within a range into a single value.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warnings"><LI>Warnings</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <numeric></PRE>
<PRE>template <class InputIterator, class T>
T accumulate (InputIterator first,
InputIterator last,
T init);
template <class InputIterator,
class T,
class BinaryOperation>
T accumulate (InputIterator first,
InputIterator last,
T init,
BinaryOperation binary_op);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P><B><I>accumulate</B></I> applies a binary operation to <SAMP>init</SAMP> and each value in the range <SAMP>[first,last).</SAMP> The result of each operation is returned in <SAMP>init</SAMP>. This process aggregates the result of performing the operation on every element of the sequence into a single value.</P>
<P>Accumulation is done by initializing the accumulator <SAMP>acc</SAMP> with the initial value <SAMP>init</SAMP> and then modifying it with <SAMP>acc = acc + *i </SAMP>or <SAMP>acc = binary_op(acc, *i)</SAMP> for every iterator <SAMP>i</SAMP> in the range <SAMP>[first, last)</SAMP> in order. If the sequence is empty, <B><I>accumulate</B></I> returns <SAMP>init</SAMP>.</P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P><B><I>accumulate</B></I> performs exactly <SAMP>last-first </SAMP> applications of the binary operation (<SAMP>operator+ </SAMP>by default).</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// accum.cpp
//
#include <numeric> //for accumulate
#include <vector> //for vector
#include <functional> //for times
#include <iostream.h>
int main()
{
//
//Typedef for vector iterators
//
typedef vector<int>::iterator iterator;
//
//Initialize a vector using an array of ints
//
int d1[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v1(d1, d1+10);
//
//Accumulate sums and products
//
int sum = accumulate(v1.begin(), v1.end(), 0);
int prod = accumulate(v1.begin(), v1.end(),
1, times<int>());
//
//Output the results
//
cout << "For the series: ";
for(iterator i = v1.begin(); i != v1.end(); i++)
cout << *i << " ";
cout << " where N = 10." << endl;
cout << "The sum = (N*N + N)/2 = " << sum << endl;
cout << "The product = N! = " << prod << endl;
return 0;
}
Output :
For the series: 1 2 3 4 5 6 7 8 9 10 where N = 10.
The sum = (N*N + N)/2 = 55
The product = N! = 3628800
</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>If your compiler does not support default template parameters then you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance you'll have to write:</P>
<PRE>vector<int,allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>vector<int></PRE>
<PRE>
</PRE>
<HR>
<A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="adj_9476.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -