📄 par_6923.htm
字号:
<HTML><TITLE>partial_sum</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>partial_sum</H2>
<HR><PRE> Generalized Numeric Operation</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Calculates successive partial sums of a range of values.</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="#Warning"><LI>Warning</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <numeric></PRE>
<PRE>
template <class InputIterator, class OutputIterator>
OutputIterator <B>partial_sum</B> (InputIterator first,
InputIterator last,
OutputIterator result);
template <class InputIterator,
class OutputIterator,
class BinaryOperation>
OutputIterator <B>partial_sum</B> (InputIterator first,
InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>partial_sum</B></I> algorithm creates a new sequence in which every element is formed by adding all the values of the previous elements, or, in the second form of the algorithm, applying the operation <SAMP>binary_op</SAMP> successively on every previous element. That is, <B><I>partial_sum</B></I> assigns to every iterator <SAMP>i</SAMP> in the range <SAMP>[result, result + (last - first))</SAMP> a value equal to:</P>
<PRE>((...(*first + *(first + 1)) + ... ) + *(first + (i - result)))</PRE>
<PRE></PRE><P> or, in the second version of the algorithm:</P>
<PRE>binary_op(binary_op(..., binary_op (*first, *(first + 1)),...),*(first + (i - result)))</PRE>
<PRE></PRE><P>For instance, applying <B><I>partial_sum</B></I> to (1,2,3,4,) will yield (1,3,6,10).</P>
<P>The <B><I>partial_sum</B></I> algorithm returns <SAMP>result</SAMP> <SAMP>+ (last - first)</SAMP>.</P>
<P>If <SAMP>result</SAMP> is equal to <SAMP>first</SAMP>, the elements of the new sequence successively replace the elements in the original sequence, effectively turning <B><I>partial_sum</B></I> into an inplace transformation. </P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>Exactly <SAMP>(last - first) - 1</SAMP> applications of the default <SAMP>+</SAMP> operator or <SAMP>binary_op</SAMP> are performed.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// partsum.cpp
//
#include <numeric> //for accumulate
#include <vector> //for vector
#include <functional> //for times
#include <iostream.h>
int main()
{
//Initialize a vector using an array of ints
int d1[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v(d1, d1+10);
//Create an empty vectors to store results
vector<int> sums((size_t)10), prods((size_t)10);
//Compute partial_sums and partial_products
<B>partial_sum</B>(v.begin(), v.end(), sums.begin());
<B>partial_sum</B>(v.begin(), v.end(), prods.begin(), times<int>());
//Output the results
cout << "For the series: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
cout << "The partial sums: " << endl << " " ;
copy(sums.begin(),sums.end(),
ostream_iterator<int>(cout," "));
cout <<" should each equal (N*N + N)/2" << endl << endl;
cout << "The partial products: " << endl << " ";
copy(prods.begin(),prods.end(),
ostream_iterator<int>(cout," "));
cout << " should each equal N!" << endl;
return 0;
}
Output :
For the series:
1 2 3 4 5 6 7 8 9 10
The partial sums:
1 3 6 10 15 21 28 36 45 55 should each equal (N*N + N)/2
The partial products:
1 2 6 24 120 720 5040 40320 362880 3628800 should each equal N!</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, then you need to always provide the <SAMP>Allocator</SAMP> template argument. For instance, you will need to write :</P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector<int></SAMP></P>
<HR>
<A HREF="par_1563.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="par_0264.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -