⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 acc_0611.htm

📁 ARM编辑、编译软件
💻 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>&copy;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 &#60;numeric></PRE>
<PRE>template &#60;class InputIterator, class T>
T accumulate (InputIterator first,
              InputIterator last,
              T init);
template &#60;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 &#60;numeric>   //for accumulate
 #include &#60;vector>    //for vector
 #include &#60;functional> //for times
 #include &#60;iostream.h>
 int main()
 {
   //
   //Typedef for vector iterators
   //
   typedef vector&#60;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&#60;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&#60;int>());
   //
   //Output the results
   //
   cout &#60;&#60; "For the series: ";
   for(iterator i = v1.begin(); i != v1.end(); i++)
       cout &#60;&#60; *i &#60;&#60; " ";
   cout &#60;&#60; " where N = 10." &#60;&#60; endl;
   cout &#60;&#60; "The sum = (N*N + N)/2 = " &#60;&#60; sum &#60;&#60; endl;
   cout &#60;&#60; "The product = N! = " &#60;&#60; prod &#60;&#60; 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&#60;int,allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>vector&#60;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 + -