다음을 통해 공유


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
    지정된 이진 연산에 따라 부분적으로 합을 계산하거나 결합할 범위의 첫 번째 요소를 주소 지정하는 입력 반복기입니다.

  • _Last
    반복된 누적에 실제로 포함된 마지막 요소 하나 다음 위치의 지정된 이진 연산에 따라 부분적으로 합을 계산하거나 결합할 범위의 마지막 요소를 주소 지정하는 입력 반복기입니다.

  • _Result
    일련의 부분적 합 또는 지정된 작업의 결과를 저장할 대상 범위의 첫 번째 요소를 주소 지정하는 출력 반복기입니다.

  • _Binary_op
    부분적 합 절차에서 합 연산을 대체하는 일반화된 연산에 적용할 이항 연산입니다.

반환 값

대상 연산 끝을 주소 지정하는 출력 반복기: _Result + (_Last - _First),

설명

부분 합을 계산할 수 있도록 출력 반복기 _Result는 입력 반복기 _First와 동일한 반복기일 수 있습니다.

입력 범위의 값 시퀀스 a1, a2, a3에 대해 첫 번째 템플릿 함수는 연속 부분 합을 대상 범위에 저장합니다. 여기서 i번째 요소는 ( ( (a1 + a2) + a3) ai)로 지정됩니다.

입력 범위의 값 시퀀스 a1, a2, a3에 대해 두 번째 템플릿 함수는 연속 부분 합을 대상 범위에 저장합니다. 여기서 i번째 요소는 ( ( ( a1 _Binary_op a2 ) _Binary_op a3 ) ai)로 지정됩니다.

이항 연산 _Binary_op는 적용 연산 순서가 완전히 적용되므로 결합성이 있거나 가환적일 필요가 없습니다.

예제

// 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 샘플)

표준 템플릿 라이브러리