다음을 통해 공유


adjacent_difference

각 요소와 입력 범위의 해당 선행 작업간 연속 차이를 계산하고 결과를 대상 범위로 출력하거나 차이 연산을 지정된 다른 이진 연산으로 대체한 일반화된 절차 결과를 계산합니다.

template<class InputIterator, class OutIterator> 
   OutputIterator adjacent_difference( 
      InputIterator _First,  
      InputIterator _Last, 
      OutputIterator _Result  
   ); 

template<class InputIterator, class OutIterator, class BinaryOperation> 
   OutputIterator adjacent_difference( 
      InputIterator _First,  
      InputIterator _Last, 
      OutputIterator _Result,  
      BinaryOperation _Binary_op 
   );

매개 변수

  • _First
    입력 범위에서 해당 선행 작업과 차별화해야 하거나 지정된 다른 이진 작업에서 값 쌍을 처리해야 하는 첫 번째 요소를 주소 지정하는 입력 반복기입니다.

  • _Last
    입력 범위에서 해당 선행 작업과 차별화해야 하거나 지정된 다른 이진 작업에서 값 쌍을 처리해야 하는 마지막 요소를 주소 지정하는 입력 반복기입니다.

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

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

반환 값

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

설명

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

입력 범위의 값 시퀀스 a1, a2, a3에 대해 첫 번째 템플릿 함수는 partial_differences a1, a2 - a1, a3 – a2를 대상 범위에 저장합니다.

입력 범위의 값 시퀀스 a1, a2, a3에 대해 첫 번째 템플릿 함수는 partial_differences a1, a2 _Binary_op a1, a3 _Binary_op a2를 대상 범위에 저장합니다.

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

예제

// numeric_adj_diff.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, LIterend2;

   int t;
   for ( t = 1 ; t <= 10 ; t++ )
   {
      L1.push_back( t * 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 adjacent_differences of
   // elements in a list output to a vector
   VIterend = adjacent_difference ( L1.begin ( ) , L1.end ( ) , 
      V1.begin ( ) );
   
   cout << "Output vector containing adjacent_differences is:\n ( " ;
   for ( VIter1 = V1.begin( ) ; VIter1 != VIterend ; VIter1++ )
      cout << *VIter1 << " ";
   cout << ")." << endl;

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

   // Computation of adjacent_differences in place
   LIterend2 = adjacent_difference ( L1.begin ( ) , L1.end ( ) , L1.begin ( ) );
   cout << "In place output adjacent_differences in list L1 is:\n ( " ;
   for ( LIter1 = L1.begin( ) ; LIter1 != LIterend2 ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;
}

Output

The input list L1 is:
 ( 1 4 9 16 25 36 49 64 81 100 ).
Output vector containing adjacent_differences is:
 ( 1 3 5 7 9 11 13 15 17 19 ).
The output vector with the adjacent products is:
 ( 1 4 36 144 400 900 1764 3136 5184 8100 ).
In place output adjacent_differences in list L1 is:
 ( 1 3 5 7 9 11 13 15 17 19 ).

요구 사항

헤더: <numeric>

네임스페이스: std

참고 항목

참조

adjacent_difference 및 vector::push_back

표준 템플릿 라이브러리