VOOZH about

URL: https://www.geeksforgeeks.org/cpp/numeric-header-in-c-stl-set-2-adjacent_difference-inner_product-and-iota/

⇱ numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota()) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())

Last Updated : 23 Jul, 2025

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.

Output
Result 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.

Output
Result 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

Output
 a : 100 101 102 103 104 105 106
Comment
Article Tags: