inner_product() in C++

inner_product(): This function is available in the file stl_numeric.h. This function computes the inner product of two ranges. Starting with an initial value of __init. 

Multiplies successive elements from the two ranges and adds each product into the accumulated value using operator+(). The values in the ranges are processed in the order. It returns the final inner product.

Parameters:

 __first1 – Start of range 1. 

__last1 – End of range 1. 

__first2 – Start of range 2.

 __init – Starting value to add other values to it.

Syntax:

inner_product(__first1, __last1, __first2, __init)

For Example:

arr = {1, 2, 3, 10}, vec = {4, 5, 6, 45, 10}

inner_product(arr.begin(), arr.end(), vec.begin(),0) => It return the value 482.

1*4 + 2*5 + 3*6 + 10*45 =  482

Approach

C++

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

int main()
{
    vector<intarr = {12310};
    vector<intvec = {4564510};

    cout << inner_product(arr.begin(), arr.end(), 
vec.begin(), 0<< "\n";

    return 0;
}


No comments:

Post a Comment