C++ STL算法之accumulate函数

1. 介绍

  用来计算特定范围内(包括连续的部分和初始值)所有元素的和,除此之外,还可以用指定的二进制操作来计算特定范围内的元素结果。其头文件在numeric中。 
  accumulate原函数声明定义如下: 
  

<code class="hljs delphi has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">template<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> InputIterator, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Type</span>>
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Type</span> accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Type</span> _Val
   );
template<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> InputIterator, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Type</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> Fn2>
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Type</span> accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Type</span> _Val, 
      BinaryOperation _Binary_op
   );</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

参数说明。

参数 描述
_First 指定范围内第一个迭代的值或者结合操作选项使用。
InputIterator _Last 指定范围内最后一个迭代值或者结合操作项使用。
_Val 要计算的初始值。
_Binary_op 运用于指定范围内所有元素和前面计算得到结果的参数。

但看说明也许不太容易懂,我们通过例子更具体的解释下这个函数。

2. 应用举例

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <vector></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <numeric></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <functional></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main( ) 
{

   <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> v1, v2( <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span> );
   <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span>::iterator Iter1, Iter2;

   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i;
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> ( i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> ; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> ; i++ )
   {
      v1.push_back( i );
   }

   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"最初向量v1中个元素的值为:\n ( "</span> ;
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << *Iter1 << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>;
   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">")."</span> << endl;

   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// accumulate函数的第一个功能,求和</span>
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> total;
   total = accumulate ( v1.begin ( ) , v1.end ( ) , <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> );

   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"整数从1到20的和为: "</span> 
        << total << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"."</span> << endl;

   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 构造一个前n项和的向量</span>
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, partotal;
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> ( Iter1 = v1.begin( ) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; Iter1 != v1.end( ) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> ; Iter1++ )
   {
      partotal = accumulate ( v1.begin ( ) , Iter1 , <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> );
      v2 [ j ] = partotal;
      j++;
   }

   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"前n项和分别为:\n ( "</span> ;
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << *Iter2 << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>;
   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">")."</span> << endl << endl;

   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// accumulate函数的第二个功能,计算连乘积</span>
   <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> v3, v4( <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> );
   <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span>::iterator Iter3, Iter4;

   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> s;
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> ( s = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> ; s < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span> ; s++ )
   {
      v3.push_back( s );
   }

   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"向量v3的初始值分别为:\n ( "</span> ;
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
      <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << *Iter3 << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>;
   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">")."</span> << endl;

   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> ptotal;
   ptotal = accumulate ( v3.begin ( ) , v3.end ( ) , <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> , multiplies<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>>( ) );

   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"整数1到10的连乘积为: "</span> 
        << ptotal << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"."</span> << endl;

   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 构造一个前n项积的向量</span>
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> k = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, ppartotal;
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> ( Iter3 = v3.begin( ) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; Iter3 != v3.end( ) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> ; Iter3++ ) {
      ppartotal = accumulate ( v3.begin ( ) , Iter3 , <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> , multiplies<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>>( ) );
      v4 [ k ] = ppartotal;
      k++;
   }

   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"前n项积分别为:\n ( "</span> ;
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> ( Iter4 = v4.begin( ) ; Iter4 != v4.end( ) ; Iter4++ )
      <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << *Iter4 << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" "</span>;
   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">")."</span> << endl;
}
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li></ul>

编译运行,看一下输出结果: 
C++ STL算法之accumulate函数_第1张图片

你可能感兴趣的:(C++ STL算法之accumulate函数)