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
    處理輸入的 Iterator 在項目將 differenced 與其各自的前置的輸入範圍的第一個項目的值是由另一個指定的二進位運算作業。

  • _Last
    處理輸入的 Iterator 在項目將 differenced 與其各自的前置的輸入範圍的最後一個項目的值是由另一個指定的二進位運算作業。

  • _Result
    解決輸出 Iterator 的第一個項目目的範圍要儲存位置差異或指定之作業的結果序列。

  • _Binary_op
    要套用在取代減法運算的在這個字之程序的通用作業的二進位運算。

傳回值

解決輸出 Iterator 的目的範圍的結尾: _Result + (_Last -_First).

備註

輸出Iterator _Result允許是Iterator和輸入Iterator _First相同, 因此, adjacent_differences可以就地計算。

如果數值 a1序列, a2a3,輸入範圍中,第一個樣板函式在目的範圍儲存執行 partial_differences a1a2- a1, a3 – a2,否則為。

如果數值 a1序列, a2a3,輸入範圍,第二個樣板函式在目的範圍儲存執行 partial_differences a1a2_Binary_opa1a3_Binary_opa2,否則為。

您不需要使用二進位運算 _Binary_op 是或結合或交替,,因為作業順序套用完全指定。

adjacent_difference 有兩個關聯的表單:

如果您將已檢查的Iterator為其中一 adjacent_difference表單,您簽入Iterator行為。 如果您將未核取的Iterator,您取得未核取的行為。如需詳細資訊,請參閱 檢查過的 Iterator

範例

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

標準樣板程式庫