當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ std::adjacent_difference用法及代碼示例

計算範圍的相鄰差
分配給從結果開始的範圍內的每個元素,其在[first,last]範圍內的對應元素與該元素之前的元素之間的差(除* result外,其分配為* first)。

如果x表示[first,last]中的元素,而y表示結果中的元素,則ys可計算為:

y0 = x0
y1 = x1 - x0
y2 = x2 - x1
y3 = x3 - x2
y4 = x4 - x3
and so on.

1.使用默認版本:語法:模板:


OutputIterator adjacent_difference (InputIterator first,
                                    InputIterator last,
                                    OutputIterator result);
參數:

first, last
Input iterators to the initial and final positions in a sequence.
The range used is [first, last], which contains all the elements
between first and last, including the element pointed by first but
not the element pointed by last.

result
Output iterator to the initial position in the destination sequence
where the differences are stored. The range starts at result and
shall have a size large enough to contain as many elements as the
range above [first, last]. 

返回類型:
An iterator pointing to past the last element of the destination
sequence where resulting elements have been stored.

// CPP program to illustrate 
// std::adjacent_difference 
  
#include <iostream> // std::cout 
#include <numeric> // std::adjacent_difference 
  
// Driver code 
int main() 
{ 
    int val[] = { 1, 2, 3, 5, 9, 11, 12 }; 
    int n = sizeof(val) / sizeof(val[0]); 
    int result[7]; 
  
    // Array contains 
    std::cout << "Array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << val[i]; 
    std::cout << "\n"; 
  
    // Using default std::adjacent_difference 
    std::adjacent_difference(val, val + 7, result); 
    std::cout << "Using default adjacent_difference:"; 
    for (int i = 1; i < n; i++) 
        std::cout << result[i] << ' '; 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:

Array contains:1 2 3 5 9 11 12
Using default adjacent_difference:1 1 2 4 2 1

2.使用自定義版本,以函數作為補償語法:模板:

OutputIterator adjacent_difference (InputIterator first,
                                    InputIterator last,
                                    OutputIterator result,
                                    BinaryOperation binary_op);

參數:

first, last, result are same as above.

binary_op
Binary operation taking as arguments two elements of the type
pointed by InputIterator, and returning the result of the
replacement for the difference operation.
This can either be a function pointer or a function object. 

返回類型:
An iterator pointing to past the last element of the destination
sequence where resulting elements have been stored.

通過在自定義函數中將運算符更改為任何二進製運算符,我們可以更改在STL函數上應用的運算。此處執行相鄰元素的總和。

// CPP program to illustrate 
// std::adjacent_difference 
  
#include <iostream> // std::cout 
#include <numeric> // std::adjacent_difference 
  
int comp(int x, int y) 
{ 
    return x + y; 
} 
  
// Driver code 
int main() 
{ 
    int val[] = { 1, 2, 3, 5, 9, 11, 12 }; 
    int n = sizeof(val) / sizeof(val[0]); 
    int result[7]; 
  
    // Array contains 
    std::cout << "Array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << val[i]; 
    std::cout << "\n"; 
  
    // std::adjacent_difference using custom function 
    std::adjacent_difference(val, val + 7, result, comp); 
  
    std::cout << "Using custom function:"; 
    for (int i = 0; i < n; i++) 
        std::cout << result[i] << ' '; 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:

Array contains:1 2 3 5 9 11 12
Using custom function:1 3 5 8 14 20 23 

實際應用:在所述範圍的相鄰元素之間執行任何二進製運算(範圍的第一個元素除外)。

1.查找數組中相鄰元素的乘積。
例如,數組包含:2 4 5 6
結果是:2 8 20 30
說明-第一個元素保持原樣。然後第二個元素將是第一個元素*第二個元素,然後第三個元素將是第二個元素*第三個元素,依此類推。

// CPP program to illustrate 
// std::adjacent_difference 
  
#include <iostream> // std::cout 
#include <numeric> // std::adjacent_difference 
  
int comp(int x, int y) 
{ 
    return x * y; 
} 
  
// Driver code 
int main() 
{ 
    int val[] = { 5, 7, 4, 8, 2 }; 
    int n = sizeof(val) / sizeof(val[0]); 
    int result[n]; 
  
    // Array contains 
    std::cout << "Array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << val[i]; 
    std::cout << "\n"; 
  
    // Using custom std::adjacent_difference 
    std::adjacent_difference(val, val + 7, result, comp); 
    std::cout << "Result contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << ' ' << result[i]; 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:

Array contains:5 7 4 8 2
Result contains:5 35 28 32 16


相關用法


注:本文由純淨天空篩選整理自 std::adjacent_difference in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。