inner_product
版本1:
template < class InputIterator1, class InputIterator2, class T>
T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init)
{
//以第一序列之元素个数为据,将两个序列都走一遍
for( ; first != last1; ++first1, ++first2)
init = init + (*first1 * *first2);//执行两个序列的一般内积
return init;
}
版本2:
template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2)
{
//以第一序列之元素个数为据,将两个序列都走一遍
for( ; first != last1; ++first1, ++first2)
//以外界提供的仿函数来取代第一版中的operator*和operator+
init = binary_op1(init, binary_op2(*first1, *first2));
return init;
}
算法inner_product 能够计算[first,last)和[first2, first2 +( last1 - first1))的一般内积。注意,你一定得提供初值init。这么做的原因之一是当[first,last)为空时,仍可以获得一个明确定义的结果。如果你想计算两个vector的一般内积,应该将 init 设为0。
第一个版本会将两个区间的内积结果加上init。也就是说,现将结果初始化为init,然后针对[ first1,last1 )的每一个迭代器 i ,由头至尾依序执行result = result + ( *i ) * * (first2 + ( i - first ))。
第二个版本与第一版本的唯一差异是以外界提供之仿函数来取代 operator+ 和 operator*。也就是说,首先将结果初始化为init,然后针对[first1,last1)的每一个迭代器i,由头至尾依序执行 result = binary_op1(result, binary_op2(*i, *(first2 + ( i - first1))))。
式中所用的二元仿函数不必满足交换律和结合律。
inner_product 所有运算行为的顺序都有明确规定。
inner_product的更多相关文章
- C++ STD inner_product函数
C++ STD函数 inner_product是c++标准库封装的一个函数. 函数原型: 函数1: inner_product(beg1, end1, beg2, init) 函数2: inner ...
- 一般化数值算法(accumluate,inner_product,partial_sum,adjacent_difference)
accumulate template<class InputIterator, class T> T accumulate( InputIterator _First, InputIte ...
- C++STL 中的数值算法(iota、accumulate、adjacent_difference、inner_product、partial_sum)
以下算法均包含在头文件 numeric 中 ##1.iota 该函数可以把一个范围内的序列从给定的初始值开始累加 先看用法. 例: 假设我需要一个长度为10,从5开始递增的序列 vector<i ...
- 原创 C++作用域 (一)
1概述 在所有的计算机程序中,一个基本的目标是操作一些数据,然后获得一些结果.为了操作这些数据,需要为这些数据分配一段内存,我们可以将这段内存称为变量.为了方便操作,以及程序可读性方面的考虑,需要使用 ...
- 从Bayesian角度浅析Batch Normalization
前置阅读:http://blog.csdn.net/happynear/article/details/44238541——Batch Norm阅读笔记与实现 前置阅读:http://www.zhih ...
- C++的STL
今天,看一段代码的时候发现只一句话就做了个排序,是这样的: sort(rotateArray.begin(),rotateArray.end()); 很震惊,后来查了一下sort的用法, sort函数 ...
- 【转】C++标准库和标准模板库
C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...
- C++标准库简介、与STL的关系。
转自http://www.cnblogs.com/xiongjiaji/archive/2011/06/22/2476490.html C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在5 ...
- STL---总结
文章转自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/22/2603525.html 一.STL的六大组件 容器(Container),是一种 ...
随机推荐
- iis url重写
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...
- MATLAB 程序计算结果出现 复数(a+bi)问题
存在对负数开根号的情况了: >> (0.777)^0.1 ans = 0.9751 >> ( ans = 0.6037 >> (0.777)^2.1 ans = 0 ...
- 【VS2019】F12跳转到源码,关闭浏览器不停止项目【转】
[VS2019]F12跳转到源码 1.工具->选项 2.文本编辑器->C#->高级->勾选支持导航到反编译源码 3.关闭浏览器不停止项目
- Navicat Premium 12破解方法
来源网址:https://www.jianshu.com/p/42a33b0dda9c 1.按步骤安装Navicat Premium,如果没有可以去官网下载:http://www.navicat.co ...
- “The subscription does not exist” when a distributor primary replica fails over to a replica that does not use the same agent profile
Symptoms Consider the following scenario: In Microsoft SQL Server 2017, you have a distribution agen ...
- [elk]kafka_elk
kafka https://www.tutorialspoint.com/apache_kafka/apache_kafka_cluster_architecture.htm https://dzon ...
- 为github公开项目单独设置用户名
背景: 由于在公司git的配置是公司内自己的用户id及邮箱,如果用同样的配置在github上提交公开项目,可能造成用户名及邮箱泄露,因此对于github上的项目,最好单独设置用户名,而每次在向gith ...
- users-and-groups-in-linux
https://www.tecmint.com/compress-files-and-finding-files-in-linux/ https://www.tecmint.com/manage-us ...
- linux下以‘-’开头的文件名
linux下以‘-’开头的文件名,cp.mv.rm.ls等对他都是无效的: [root@ha131 ~]# ll -plat.py ls:无效选项 -- . 请尝试执行"ls --help& ...
- Ubuntu apt 使用代理
. . . . . 由于一台 Ubuntu 主机无法直接连接外网,但同内网的另一台 Windows 主机可以连接外网,于是在 Win 主机上搭建了代理服务,使 Ubuntu 主机通过代理更新系统. 记 ...