numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
Last Updated :
17 Jul, 2022
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:
- iota
- accumulate
- reduce
- inner_product
- partial_sum etc.
See this article for more reference: accumulate() and partial_sum() in C++ STL : Numeric header
This article explains adjacent_difference(), inner_product(), and iota in the numeric header which can be used during competitive programming to save time and effort.
1) adjacent_difference(): This function assigns the difference between the corresponding elements of an array to another array. It returns the adjacent difference of all the sets of values lying between [ First, last ).
For Example: If a[] represents an element in the provided range [first, last) and b[] represents the result.
b[0] = a[0]
b[1] = a[1] – a[0]
b[2] = a[2] – a[1]
b[3] = a[3] – a[2]
b[4] = a[4] – a[3]
... ... ...
Syntax:
adjacent_difference(first, last, b);
adjacent_difference(first, last, b, myfun );
adjacent_difference(first, last, b, multiplies() );
Parameters:
- first, last: address of first and last element of range whose elements are to be added
- b: index of array where corresponding partial sum will be stored;
- myfun: a user-defined function for performing any specific task
- multiplies(): a pre-defined function.
CPP
// CPP Program to demonstrate adjacent_difference()
#include <functional>
#include <iostream>
#include <numeric>
using namespace std;
int myfun(int x, int y) { return x + y; }
// Driver Code
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int b[6];
// using adjacent_difference function
adjacent_difference(a, a + 6, b);
cout << "\nResult using adjacent_difference: ";
for (int i = 0; i < 6; i++)
std::cout << b[i] << ' ';
// using adjacent_difference function
// user defined function
adjacent_difference(a, a + 6, b, myfun);
cout << "\nResult using accumulate with user-"
"defined function: ";
for (int i = 0; i < 6; i++)
std::cout << b[i] << ' ';
// using adjacent_difference with pre-defined function
adjacent_difference(a, a + 6, b, multiplies<int>());
cout << "\nResult using accumulate with pre-defined "
"function: ";
for (int i = 0; i < 6; i++)
std::cout << b[i] << ' ';
return 0;
}
OutputResult using adjacent_difference: 1 1 1 1 1 1
Result using accumulate with user-defined function: 1 3 5 7 9 11
Result using accumulate with pre-defined function: 1 2 6 12 20 30
2) inner_product(): This function returns the result of the addition of var with the inner products of the pairs formed by the elements of two ranges starting at first1 and first2.
Syntax:
inner_product(first, last, b, var) ;
inner_product(a, a+3, b, var, fun, fun1) ;
inner_product(a , a+3, b, init, minus (), divides () );
Parameters:
- first, last: address of first and last element of range whose elements are to be added
- b: index of array where corresponding partial sum will be stored;
- fun, fun1: a user-defined function for performing any specific task
- minus(), divides(): pre defined function.
CPP
// CPP Program to demonstrate inner_product()
#include <functional>
#include <iostream>
#include <numeric>
using namespace std;
int fun(int x, int y) { return x - y; }
int fun1(int x, int y) { return x + y; }
// Driver Code
int main()
{
int var = 200;
int a[] = { 10, 15, 20 };
int b[] = { 1, 3, 5 };
cout << "\nResult using inner_product ";
// inner_product with default method
cout << inner_product(a, a + 3, b, var);
// inner_product with pre-defined function
cout << "\nResult using inner_product with pre-defined "
"function: ";
cout << inner_product(a, a + 3, b, var, minus<int>(),
divides<int>());
// inner_product with user defined function
cout << "\nResult using inner_product with "
"user-defined function: ";
cout << inner_product(a, a + 3, b, var, fun, fun1);
return 0;
}
OutputResult using inner_product 355
Result using inner_product with pre-defined function: 181
Result using inner_product with user-defined function: 146
3) iota(): This function assigns a value to the elements in the range [first, last ) of the array which is incremented at each step by val++.
Syntax:
iota(first, last,val) ;
Parameters:
- first, last: address of first and last element of range whose elements are to be added
- val: initial value to store, the expression ++value must be well-formed
CPP
// CPP Program to demonstrate iota()
#include <iostream>
#include <numeric>
using namespace std;
// Driver Code
int main()
{
int a[7];
// using iota function to store 100, 101, 102,...
iota(a, a + 7, 100);
cout << " a : ";
for (int& x : a)
cout << ' ' << x;
return 0;
}
Output a : 100 101 102 103 104 105 106
Similar Reads
C++ STL Algorithm Library
Standard Template Library (STL) offers a rich collection of algorithms designed to operate on STL containers and beyond. It provides commonly used algorithms such as sorting, searching, copying, etc. These well tested algorithms are optimized for performance and provide a way to write cleaner, faste
3 min read
sort() in C++ STL
In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include
4 min read
Type Inference in C++ (auto and decltype)
Type Inference is a feature in C++, using which the compiler automatically deduces the data type of an expression, function, or variable. Type inference was introduced with C++11 through the use of the auto and decltype.Before C++ 11, each data type had to be explicitly declared, which limited the v
5 min read
transform() in C++ STL
In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i
4 min read
Variadic function templates in C++
Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue. Douglas Gregor
3 min read
Template Specialization in C++
Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type. What if we want a different code for a p
5 min read
Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
Some of the merge operation classes are provided in C++ STL under the header file "algorithm", which facilitates several merge operations in a easy manner. Some of them are mentioned below. merge(beg1, end1, beg2, end2, beg3) :- This function merges two sorted containers and stores in new container
7 min read
std::partition in C++ STL
C++ has a class in its STL algorithms library which allows us easy partition algorithms using certain inbuilt functions. Partition refers to act of dividing elements of containers depending upon a given condition. Partition operations :1. partition(beg, end, condition) :- This function is used to pa
5 min read
accumulate() and partial_sum() in C++ STL
accumulate() and partial_sum() functions are used to find the sum or any other accumulated value that is obtained by doing the addition or any other binary operation on the elements in the given range. Both of these functions are the part of STL Numeric Library and defined inside <numeric> hea
4 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