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.
1. Using default version :
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.
Array contains : 1 2 3 5 9 11 12 Using default adjacent_difference: 1 1 2 4 2 1
2. Using custom version, taking function as comp
OutputIterator adjacent_difference (InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); Parameters : 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. Return Type : An iterator pointing to past the last element of the destination sequence where resulting elements have been stored.
By changing the operator to any binary operator in the custom function, we can change the operation that is applied on the STL function. Here the sum of adjacent elements is performed.
Array contains : 1 2 3 5 9 11 12 Using custom function: 1 3 5 8 14 20 23
Practical application : Perform any binary operation between the adjacent elements of the range mentioned (except the first element of the range).
1. Find product of the adjacent elements in the array.
For example, array contains : 2 4 5 6
Result is : 2 8 20 30
Explanation – First elements remain as it is. Then second element will be first element * second element, then third element will be second element * third element and so on.
Array contains : 5 7 4 8 2 Result contains : 5 35 28 32 16
This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Program to implement Separate Chaining in C++ STL without the use of pointers
- Speed up Code executions with help of Pragma in C/C++
- Vector of Vectors in C++ STL with Examples
- Array of Vectors in C++ STL
- Difference between Virtual function and Pure virtual function in C++
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- C++ program to print all Even and Odd numbers from 1 to N
- Reverse the content of a file and store it in another
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples