C++ STL算法之accumulate函数
生活随笔
收集整理的這篇文章主要介紹了
C++ STL算法之accumulate函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 介紹
用來計算特定范圍內(包括連續的部分和初始值)所有元素的和,除此之外,還可以用指定的二進制操作來計算特定范圍內的元素結果。其頭文件在numeric中。?
accumulate原函數聲明定義如下:?
參數說明。
| _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函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: KMP字符串模式匹配详解
- 下一篇: 拉格朗日对偶性和似然函数