泛型算法系列25:inner_product()
2009-08-18 11:11
218 查看
#include <numeric> #include <vector> #include <iostream> using namespace std; /************************************************************************/ /* */ /************************************************************************/ template<class _InIt1, class _InIt2, class _Ty> inline _Ty my_Inner_product(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _Ty _Val) { // return inner product of sequences _DEBUG_RANGE(_First1, _Last1); for (; _First1 != _Last1; ++_First1, ++_First2) _Val = _Val + *_First1 * *_First2; return (_Val); } /************************************************************************/ /* */ /************************************************************************/ template<class _InIt1, class _InIt2, class _Ty, class _Fn21, class _Fn22> inline _Ty my_Inner_product(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _Ty _Val, _Fn21 _Func1, _Fn22 _Func2) { // return inner product of sequences, using _Func1 and _Func2 _DEBUG_RANGE(_First1, _Last1); _DEBUG_POINTER(_Func1); _DEBUG_POINTER(_Func2); for (; _First1 != _Last1; ++_First1, ++_First2) _Val = _Func1(_Val, _Func2(*_First1, *_First2)); return (_Val); } int main() { int ia[] = { 2, 3, 5, 8 }; int ia2[] = { 1, 2, 3, 4 }; // multiply the element pair from the two arrays // then add to the initial value: 0 int res = my_Inner_product( &ia[0], &ia[4], &ia2[0], 0); // generates: inner product of arrays: 55 cout << "inner product of arrays: " << res << endl; vector<int> vec( ia, ia + 4 ); vector<int> vec2( ia2, ia2 + 4 ); // add the element pair from the two vectors // then subtract from the initial value: 0 res = my_Inner_product( vec.begin(), vec.end(), vec2.begin(), 0, minus<int>(), plus<int>() ); // generates: inner product of vectors: -28 cout<<"inner product of vectors: " << res << endl; return 0; }
相关文章推荐
- STL系列------算法---------inner_product
- 【剑指offer系列】 二叉树中和为某一值的路径___25
- 《Java解惑》系列——03循环谜题——谜题25:无情的增量操作(表达式中变量赋值不要超过一次)
- 泛型算法系列(01)accumulate()
- 深入理解JavaScript系列(25):设计模式之单例模式
- Java入门系列-25-NIO(实现非阻塞网络通信)
- CAFFE源码学习笔记之内积层-inner_product_layer
- linux 命令系列之 passwd(25)
- Docker系列教程25-练习:使用Docker Compose编排WordPress博客
- TRIZ系列-创新原理-25-自服务原理
- Learning Caffe(5)---全连接层inner_product_layer
- 深入理解JavaScript系列(25):设计模式之单例模式
- 敏捷开发产品管理系列之六:Product Servant
- (翻译)Entity Framework技巧系列之六 - Tip 20 – 25
- Articles from the inner product
- 深入理解JavaScript系列(25):设计模式之单例模式
- 25 union代替or --优化主题系列
- Android Api demo系列(25) (Graphics>shadow card stack)
- STL源码剖析之算法:inner_product
- 泛型算法系列2:adjacent_difference()