다음을 통해 공유


partial_sum

일련의 합계는 입력 범위의 첫 번째 요소부터 계산의 ith 요소에서 이러한 각 합계의 결과 저장 하 고는 ith 요소의 대상 범위 또는 계산 결과 합계 작업 대체 다른 일반화 된 프로시저의 지정한 이항 연산.

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로 지정 하 여 부분 합계에서 계산할 수 있습니다.

일련의 값에 대 한 1, 2, 3, 입력된 범위의 첫 번째 템플릿 함수가 연속 부분 합계 대상 범위에 저장 위치는 ith 요소에서 지정 됩니다 (((1 + 2) + 3) i).

일련의 값에 대 한 1, 2, 3, 입력된 범위의 두 번째 템플릿 함수에서 i 번째 요소입니다 주어진 위치에서 대상 범위, 연속 부분 합계 저장 ((( 1_Binary_op2 ) _Binary_op3 ) i).

이항 연산의 _Binary_op 연관 또는 비가 환 적 될 필요가 없습니다, 작업 순서에 적용 되기 때문에 완전 하 게 지정 됩니다.

partial_sum두 폼 관련 있습니다.

양식 중의 하나는 확인 된 반복기를 전달할 경우 partial_sum, 동작 확인 된 반복기를 가져옵니다.  선택 되지 않은 반복기를 전달 하는 경우 확인 되지 않은 동작을 볼 수 있습니다.자세한 내용은 확인 된 반복기를 참조하십시오.

예제

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

표준 템플릿 라이브러리