How to use default version Syntax Template In C++

OutputIterator adjacent_difference (InputIterator first,
InputIteratorlast,
OutputIterator result);
Parameters :
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].
Return Type :
An iterator pointing to past the last element of the destination
sequence where resulting elements have been stored.
CPP
// 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;
}

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

Time Complexity: O(n)

Space Complexity: O(n)

std::adjacent_difference in C++

Compute adjacent difference of range Assigns to every element in the range starting at result, the difference between its corresponding element in the range [first, last] and the one preceding it (except for *result, which is assigned *first). If x represents an element in [first, last] and y represents an element in result, the ys can be calculated as:

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

Similar Reads

1. Using default version : Syntax : Template :

OutputIterator adjacent_difference (InputIterator first, InputIteratorlast, OutputIterator result);Parameters :first, lastInput iterators to the initial and final positions in a sequence.The range used is [first, last], which contains all the elementsbetween first and last, including the element pointed by first butnot the element pointed by last.resultOutput iterator to the initial position in the destination sequencewhere the differences are stored. The range starts at result andshall have a size large enough to contain as many elements as therange above [first, last]. Return Type :An iterator pointing to past the last element of the destinationsequence where resulting elements have been stored....

2. Using custom version, taking function as comp Syntax : Template :

OutputIterator adjacent_difference (InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);Parameters :first, last, result are same as above.binary_opBinary operation taking as arguments two elements of the typepointed by InputIterator, and returning the result of thereplacement for the difference operation.This can either be a function pointer or a function object. Return Type :An iterator pointing to past the last element of the destinationsequence where resulting elements have been stored....