adjacent_difference() in C++

adjacent_difference(): This is a build-in function in the STL. This function is 

available in the file stl_numeric.h. This function return differences between adjacent values. 

Computes the difference between adjacent values in the range [first, last) using 

operator-() and writes the result to __result.

Parameters: Three parameters are required for this function.

__first – Start of the input range.

 __last – End of the input range.

 __result – Output sums.

Syntax:

adjacent_difference(__first, __last, __result)

For Example:

vec = {1, 4, 7, 9, 1, 6, 9, 10}, arr

adjacent_difference(vec,begin(),vec.end(),arr,begin()

arr ={1 ,3 ,3 ,2 ,-8, 5, 3, 1} 

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<intvec = {147916910};
    vector<intarr(vec.size());

    adjacent_difference(vec.begin(), vec.end(), arr.begin());

    for (int i = 0i < arr.size(); i++)
        cout << arr[i] << " ";

    return 0;
}


No comments:

Post a Comment