共用方式為


partial_sum

透過 第 i 個項目在目的範圍的 第 i 個項目計算輸入範圍的一系列的總和 (從第一個項目並儲存每一個這類的總和的結果或計算總和作業由另一個指定的二進位運算取代的通用程序的結果。

template<class InputIterator, class OutIt>
   OutputIterator partial_sum(
      InputIterator _First, 
      InputIterator _Last,
      OutputIterator _Result
   );

template<class InputIterator, class OutIt, class Fn2>
   OutputIterator partial_sum(
      InputIterator _First, 
      InputIterator _Last,
      OutputIterator _Result, 
      BinaryOperation _Binary_op
   );

參數

  • _First
    處理輸入的 Iterator 在根據指定的二進位運算部分輸入序列或合併的範圍中的第一個項目。

  • _Last
    處理輸入的 Iterator 在根據是在最後一個項目以外的某個位置的其中一個指定的二進位運算部分輸入序列或合併的範圍的最後一個項目在中逐一查看累積實際上由。

  • _Result
    解決輸出 Iterator 的第一個項目目的範圍要存放的本機部分和或指定之作業的結果序列。

  • _Binary_op
    要套用在取代總和作業區段與程序的通用作業的二進位運算。

傳回值

解決輸出 Iterator 的目的範圍的結尾: _Result + (_Last -_First),

備註

輸出 Iterator _Result 允許是 Iterator 和輸入 Iterator _First,因此,部分可以就地計算。

如果是值 1順序, 2, 3,則輸入範圍,第一個樣板函式在目的範圍儲存執行部分,提供 第 i 個項目 (((+1*)*2+ *)3)*我。

如果是值 1順序, 2, 3,則輸入範圍,第二個樣板函式在目的範圍儲存執行部分,提供第 i 個項目 (_Binary_op((1*)2_Binary_op)3)*我。

您不需要使用二進位運算 _Binary_op 是或結合或交替,,因為作業順序套用完全指定。

partial_sum 有兩個關聯的表單:

如果您將已檢查的 Iterator 為其中一 partial_sum表單,您簽入 Iterator 行為。 如果您將未核取的 Iterator,您取得未核取的行為。 如需詳細資訊,請參閱 檢查過的 Iterator

範例

// numeric_partial_sum.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>

int main( ) 
{
   using namespace std;   
   vector<int> V1( 10 ), V2( 10 );
   vector<int>::iterator VIter1, VIter2, VIterend, VIterend2;

   list <int> L1;
   list <int>::iterator LIter1, LIterend;

   int t;
   for ( t = 1 ; t <= 10 ; t++ )
   {
      L1.push_back( t );
   }

   cout << "The input list L1 is:\n ( " ;
   for ( LIter1 = L1.begin( ) ; LIter1 != L1.end( ) ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;

   // The first member function for the partial sums of
   // elements in a list output to a vector
   VIterend = partial_sum ( L1.begin ( ) , L1.end ( ) , 
      V1.begin ( ) );
   
   cout << "The output vector containing the partial sums is:\n ( " ;
   for ( VIter1 = V1.begin( ) ; VIter1 != VIterend ; VIter1++ )
      cout << *VIter1 << " ";
   cout << ")." << endl;

   // The second member function used to compute
   // the partial product of the elements in a list
   VIterend2 = partial_sum ( L1.begin ( ) , L1.end ( ) , V2.begin ( ) , 
      multiplies<int>( ) );
   
   cout << "The output vector with the partial products is:\n ( " ;
   for ( VIter2 = V2.begin( ) ; VIter2 != VIterend2 ; VIter2++ )
      cout << *VIter2 << " ";
   cout << ")." << endl;

   // Computation of partial sums in place
   LIterend = partial_sum ( L1.begin ( ) , L1.end ( ) , L1.begin ( ) );
   cout << "The in place output partial_sum list L1 is:\n ( " ;
   for ( LIter1 = L1.begin( ) ; LIter1 != LIterend ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;
}

Output

The input list L1 is:
 ( 1 2 3 4 5 6 7 8 9 10 ).
The output vector containing the partial sums is:
 ( 1 3 6 10 15 21 28 36 45 55 ).
The output vector with the partial products is:
 ( 1 2 6 24 120 720 5040 40320 362880 3628800 ).
The in place output partial_sum list L1 is:
 ( 1 3 6 10 15 21 28 36 45 55 ).

需求

標題: <numeric>

命名空間: std

請參閱

參考

partial_sum (STL Samples)

標準樣板程式庫