다음을 통해 공유


partial_sum (STL Samples)

사용 하는 방법을 보여 줍니다 있는 partial_sum Visual C++에서 표준 템플릿 라이브러리 (STL) 함수입니다.

template<class InputIterator, class OutputIterator> inline
   OutputIterator partial_sum(
      InputIterator First,
      InputIterator Last,
      OutputIterator First2
   )
template<
   class InputIterator,
   class OutputIterator,
   class BinaryOperator
> inline
   OutputIterator partial_sum(
      InputIterator First,
      InputIterator Last,
      OutputIterator First2,
      BinaryOperator Binary_Op
   )

설명

[!참고]

프로토타입에 클래스/매개 변수 이름은 헤더 파일에서 버전이 일치 하지 않습니다.일부 가독성을 높이기 위해 수정 되었습니다.

모든 반복기를 할당 i 범위에서 [결과,결과 + (Last - First34) 값 집합이 동일 합니다 ((...(*First + *(First + 1)) + ...) + *(First + (i - Result))) - or - Binary_Op(Binary_Op(..., Binary_Op(*First, *(First + 1)),...), *(First + (i - 결과))).In other words, *(Result + i) = init + *(First + 0) + *(First + 1) + ...+ *(First + i).

예제

// partial_sum.cpp
// compile with: /EHsc
// Demonstrates the use of partial_sum().
//
// Description of partial_sum(first,last,first2,init)
//                partial_sum(first,last,first2,init,binary_op):
//
//    Assigns to every iterator i in the range
//    [result,result + (last - first)) a value correspondingly equal to
//    ((...(*first + *(first + 1)) + ...) + *(first + (i - result)))
//
//     - or -
//
//    binary_op(binary_op(..., binary_op(*first, *(first  +  1)),...),
//    *(first + (i - result)))
//
//    In other words,
//    *(result+i) = init + *(first+0) + *(first+1) + ... + *(first+i)
///////////////////////////////////////////////////////////////////////

#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>

using namespace std;

typedef vector < int > IntArray;
typedef ostream_iterator < int, char, char_traits<char> > IntOstreamIt;

int main ()
{
    IntOstreamIt itOstream(cout," ");

    // Initialize the array
    IntArray rgI;
    for (int i=1; i<=10; i++) 
      rgI.push_back(i);

    // Print the arrays
    cout << "Array: [ ";
    copy(rgI.begin(),rgI.end(),itOstream);
    cout << "]" << endl;


    // The result array must be at least the same size as the data array
    IntArray rgIresult(rgI.size());

    // Compute the partial sum of the array
    partial_sum(rgI.begin(),rgI.end(),rgIresult.begin());

    // Print the array of partial sums
    cout << "Array of partial sums: [ ";
    copy(rgIresult.begin(),rgIresult.end(),itOstream);
    cout << "]" << endl;

    // Compute the partial product of the array
    partial_sum(rgI.begin(),rgI.end(),rgIresult.begin(),multiplies<int>());

    // Print the array of partial products
    cout << "Array of partial products: [ ";
    partial_sum(rgIresult.begin(),rgIresult.end(),itOstream);
    cout << "]" << endl;
}

Output

Array: [ 1 2 3 4 5 6 7 8 9 10 ]
Array of partial sums: [ 1 3 6 10 15 21 28 36 45 55 ]
Array of partial products: [ 1 3 9 33 153 873 5913 46233 409113 4037913 ]

요구 사항

헤더: <numeric>

참고 항목

개념

표준 템플릿 라이브러리 샘플