计算范围的累积内积 返回将init与从first1和first2开始的两个范围的元素组成的对的内积累加的结果。 这两个默认操作(将对相乘的结果相加)可能会被参数binary_op1和binary_op2覆盖。 1. 使用默认的内部产品: 语法:
null
Template :T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);Parameters :first1, last1Input iterators to the initial and final positions in the firstsequence.first2Input iterator to the initial position in the second sequence.The range starts at first2 and has as many elements as the rangeabove [first1, last1].initInitial value for the accumulator.Neither operations shall modify any of the elements passed asits arguments.Return Type :The result of accumulating init and the products of all the pairsof elements in the ranges starting at first1 and first2.
CPP
// CPP program to illustrate // std :: inner_product #include <iostream> // std::cout #include <functional> // std::minus, std::divides #include <numeric> // std::inner_product // Driver code int main() { // The value which is added after // finding inner_product b/w elements int init = 100; int series1[] = { 10, 20, 30 }; int series2[] = { 1, 2, 3 }; int n = sizeof (series1) / sizeof (series1[0]); // Elements in series1 std::cout << "First array contains :" ; for ( int i = 0; i < n; i++) std::cout << " " << series1[i]; std::cout << "" ; // Elements in series2 std::cout << "Second array contains :" ; for ( int i = 0; i < n; i++) std::cout << " " << series2[i]; std::cout << "" ; std::cout << "Using default inner_product: " ; std::cout << std::inner_product(series1, series1 + n, series2, init); std::cout << '' ; return 0; } |
输出:
First array contains : 10 20 30Second array contains : 1 2 3Using default inner_product: 240
2. 使用功能操作: 语法:
Template :T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);Parameters :first1, last1, first2, init are same as above.binary_op1Binary operation taking two elements of type T as arguments, andreturning the result of an accumulation operation.This can either be a function pointer or a function object.binary_op2Binary operation taking two elements of type T as arguments, andreturning the result of the inner product operation.This can either be a function pointer or a function object.Here binary_op1 and binary_op2 are functional operation.Neither operations shall modify any of the elements passed asits arguments.Return Type :The result of accumulating init and the products of all the pairsof elements in the ranges starting at first1 and first2.
CPP
// CPP program to illustrate // std :: inner_product #include <iostream> // std::cout #include <functional> // std::minus, std::divides #include <numeric> // std::inner_product // Driver code int main() { // The value which is added after // finding inner_product b/w elements int init = 100; int series1[] = { 10, 20, 30 }; int series2[] = { 1, 2, 3 }; int n = sizeof (series1) / sizeof (series1[0]); // Elements in series1 std::cout << "First array contains :" ; for ( int i = 0; i < n; i++) std::cout << " " << series1[i]; std::cout << "" ; // Elements in series2 std::cout << "Second array contains :" ; for ( int i = 0; i < n; i++) std::cout << " " << series2[i]; std::cout << "" ; std::cout << "Using functional operations: " ; // std :: minus returns the difference b/w // each elements of both array // std :: divides return the quotient of // each elements of both array after performing // divide operation // The operations is performed b/w number of same index // of both array std::cout << std::inner_product(series1, series1 + n, series2, init, std::minus< int >(), std::divides< int >()); std::cout << '' ; return 0; } |
输出:
First array contains : 10 20 30Second array contains : 1 2 3Using functional operations: 70
3. 使用自定义函数: 语法:
Template :T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);Parameters :first1, last1, first2, init are same as above.binary_op1Binary operation taking two elements of type T as arguments, andreturning the result of an accumulation operation.This can either be a function pointer or a function object.binary_op2Binary operation taking two elements of type T as arguments, andreturning the result of the inner product operation.This can either be a function pointer or a function object.Neither operations shall modify any of the elements passed asits arguments.Return Type :The result of accumulating init and the products of all the pairsof elements in the ranges starting at first1 and first2.
CPP
// CPP program to illustrate // std :: inner_product #include <iostream> // std::cout #include <functional> // std::minus, std::divides #include <numeric> // std::inner_product // Custom functions int myaccumulator( int x, int y) { return x - y; } int myproduct( int x, int y) { return x + y; } // Driver code int main() { // The value which is added after // finding inner_product b/w elements int init = 100; int series1[] = { 10, 20, 30 }; int series2[] = { 1, 2, 3 }; int n = sizeof (series1) / sizeof (series1[0]); // Elements in series1 std::cout << "First array contains :" ; for ( int i = 0; i < n; i++) std::cout << " " << series1[i]; std::cout << "" ; // Elements in series2 std::cout << "Second array contains :" ; for ( int i = 0; i < n; i++) std::cout << " " << series2[i]; std::cout << "" ; std::cout << "Using custom functions: " ; std::cout << std::inner_product(series1, series1 + 3, series2, init, myaccumulator, myproduct); std::cout << '' ; return 0; } |
输出:
First array contains : 10 20 30Second array contains : 1 2 3Using custom functions: 34
注: 通过使用 函数值 和 自定义函数 ,我们可以通过改变STL函数中的运算符(或使用不同的函数值)来执行操作。 可能的应用: 它返回init与从first1和first2开始的两个范围的元素组成的对的内积累加的结果。 1.它可以用来求两个数组的第i个索引的乘积之和。 例如: 阵列1:1234 阵型2:10203040 产品总数:300 说明: 1 * 10 + 2 * 20 + 3 * 30 + 4 * 40 = 300
CPP
// CPP program to illustrate // std :: inner_product #include <iostream> // std::cout #include <functional> // std::minus, std::divides #include <numeric> // std::inner_product // Custom functions int myaccumulator( int x, int y) { return x + y; } int myproduct( int x, int y) { return x * y; } // Driver code int main() { // The value which is added after // finding inner_product b/w elements int init = 0; int series1[] = { 1, 2, 3, 4 }; int series2[] = { 10, 20, 30, 40 }; int n = sizeof (series1) / sizeof (series1[0]); // Elements in series1 std::cout << "Array 1 :" ; for ( int i = 0; i < n; i++) std::cout << " " << series1[i]; std::cout << "" ; // Elements in series2 std::cout << "Array 2 :" ; for ( int i = 0; i < n; i++) std::cout << " " << series2[i]; std::cout << "" ; std::cout << "Sum of products : " ; std::cout << std::inner_product(series1, series1 + n, series2, init, myaccumulator, myproduct); std::cout << '' ; return 0; } |
输出:
Array 1 : 1 2 3 4Array 2 : 10 20 30 40Sum of products : 300
我们也可以通过改变算子来求乘积的差,或除法的和,或除法的差等等。 本文由 萨钦·比什特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END