std::adjacent_difference in C++
Last Updated :
12 Apr, 2024
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 : Syntax : Template :
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;
}
OutputArray 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)
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_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.
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)
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.
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[] = { 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;
}
OUTPUT :
Array contains : 5 7 4 8 2
Result contains : 5 35 28 32 16
Time Complexity: O(n)
Space Complexity: O(n)
Similar Reads
std::adjacent_find in C++
Searches the range [first, last) for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found. Elements are compared using the given binary predicate p or using ==. There are two possible implementations
3 min read
std::set_difference in C++
The difference of two sets is formed by the elements that are present in the first set, but not in the second one. The elements copied by the function come always from the first range, in the same order. The elements in the both the ranges shall already be ordered. 1. Comparing elements using â<"
5 min read
std::advance in C++
std::advance advances the iterator 'it' by n element positions. Syntax : template void advance (InputIterator& it, Distance n); it : Iterator to be advanced n : Number of element positions to advance. This shall only be negative for random-access and bidirectional iterators. Return type : None.
2 min read
std::distance in C++
The std::distance() in C++ STL is a built-in function used to calculate the number of elements between two iterators. It is defined inside <iterator> header file. In this article, we will learn about the std::distance function in C++ with examples. Example: [GFGTABS] C++ // C++ Program to illu
5 min read
std::next vs std::advance in C++
std::advance and std::next are used to advance the iterator by a certain position, such that we can make the iterator point to a desired position. Although both have same purpose, but their implementation is different from each other. This makes it important for us to understand the difference betwe
3 min read
std::min in C++
The std::min() is used to find the minimum element among the given elements. It is the built-in function of C++ STL defined inside <algorithm> header file. In this article, we will learn how to use std::min() function in C++. std::min() Function Signaturetemplate< class T, class Compare
3 min read
numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc.
4 min read
std::set_intersection in C++
The intersection of two sets is formed only by the elements that are present in both sets. The elements copied by the function come always from the first range, in the same order. The elements in the both the ranges shall already be ordered.Examples: Input : 5 10 15 20 25 50 40 30 20 10 Output : The
5 min read
fill() and fill_n() functions in C++ STL
A vector, once declared, has all its values initialized to zero. Following is an example code to demonstrate the same. C/C++ Code // C++ program for displaying the default initialization // of the vector vect[] #include<bits/stdc++.h> using namespace std; int main() { // Creating a vec
3 min read
list::front() and list::back() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::front() This function is used to reference
3 min read