accumulate

일부 초기 값을 포함 하 여 연속 부분 합계를 계산 하 여 지정한 범위에 있는 모든 요소의 합계를 계산 하거나 마찬가지로 지정 된 합계 이외의 이항 연산 연속 부분 결과의 결과 계산 합니다.

template<class InputIterator, class Type>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val
   );
template<class InputIterator, class Type, class BinaryOperation>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val, 
      BinaryOperation _Binary_op
   );

매개 변수

  • _First
    지정 된 이항 연산에 따라 결합 하거나 합계 범위에서 첫 번째 요소의 주소 입력된 반복기입니다.

  • _Last
    한 위치에서 벗어나 실제로 이터레이션되며 누적에 포함 된 마지막 요소는 지정 된 이항 연산에 따라 결합 하거나 합계 주소 범위의 마지막 요소 입력된 반복기입니다.

  • _Val
    각 요소에 추가 하거나 지정 된 이항 연산에 따라 함께 초기 값입니다.

  • _Binary_op
    지정 된 범위 및 결과 이전 응용 프로그램의 각 요소에 적용 되는 이항 연산

반환 값

합계 _Val 및 첫 번째 템플릿 함수를 하거나 이진 지정, 합계 작업 대신에 작업을 적용 한 결과 두 번째 템플릿 함수는 지정한 범위에서 모든 요소 (PartialResult, * 반복 계산) 여기서 PartialResult 이전 응용 프로그램의 작업의 결과입니다 및 Iter 범위에 있는 요소를 가리키는 반복기입니다.

설명

된다는 결과 잘 정의 된 범위의 경우에 비어 있을 때 초기 값에 대 한 _Val 이 반환 됩니다.이항 연산의 연관 또는 비가 환 적 될 필요가 없습니다.결과 초기 값으로 초기화 _Val 다음 결과 = _Binary_op (결과, *****Iter) 범위를 통해 반복적으로 계산 됩니다 위치 Iter 범위의 연속 요소를 가리키는 반복기입니다.범위에서 유효 해야 하 고 복잡 한 선형의 범위 크기입니다.이항 연산자의 반환 형식으로 변환 될 수 있어야 유형 클로저 반복 과정을 확인 합니다.

예제

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

int main( )
{
   using namespace std;

   vector <int> v1, v2(20);
   vector <int>::iterator iter1, iter2;

   int i;
   for (i = 1; i < 21; i++)
   {
      v1.push_back(i);
   }

   cout << "The original vector v1 is:\n ( " ;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
      cout << *iter1 << " ";
   cout << ")." << endl;

   // The first member function for the accumulated sum
   int total;
   total = accumulate(v1.begin(), v1.end(), 0);

   cout << "The sum of the integers from 1 to 20 is: "
        << total << "." << endl;

   // Constructing a vector of partial sums
   int j = 0, partotal;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
   {
      partotal = accumulate(v1.begin(), iter1 + 1, 0);
      v2[j] = partotal;
      j++;
   }

   cout << "The vector of partial sums is:\n ( " ;
   for (iter2 = v2.begin(); iter2 != v2.end(); iter2++)
      cout << *iter2 << " ";
   cout << ")." << endl << endl;

   // The second member function for the accumulated product
   vector <int> v3, v4(10);
   vector <int>::iterator iter3, iter4;

   int s;
   for (s = 1; s < 11; s++)
   {
      v3.push_back(s);
   }

   cout << "The original vector v3 is:\n ( " ;
   for (iter3 = v3.begin(); iter3 != v3.end(); iter3++)
      cout << *iter3 << " ";
   cout << ")." << endl;

   int ptotal;
   ptotal = accumulate(v3.begin(), v3.end(), 1, multiplies<int>());

   cout << "The product of the integers from 1 to 10 is: "
        << ptotal << "." << endl;

   // Constructing a vector of partial products
   int k = 0, ppartotal;
   for (iter3 = v3.begin(); iter3 != v3.end(); iter3++) {
      ppartotal = accumulate(v3.begin(), iter3 + 1, 1, multiplies<int>());
      v4[k] = ppartotal;
      k++;
   }

   cout << "The vector of partial products is:\n ( " ;
   for (iter4 = v4.begin(); iter4 != v4.end(); iter4++)
      cout << *iter4 << " ";
   cout << ")." << endl;
}
  
  
  
  
  
  

요구 사항

헤더: <numeric>

네임 스페이스: std

참고 항목

참조

accumulate, copy, 및 vector::push_back

표준 템플릿 라이브러리