STL之inner_product
需要的头文件:
numeric
源码:
//版本1
template <class _InputIterator1, class _InputIterator2, class _Tp>
_Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,_InputIterator2 __first2, _Tp __init)
{for ( ; __first1 != __last1; ++__first1, ++__first2)__init = __init + (*__first1 * *__first2);return __init;
}
//版本2
template <class _InputIterator1, class _InputIterator2, class _Tp,class _BinaryOperation1, class _BinaryOperation2>
_Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,_InputIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1,_BinaryOperation2 __binary_op2)
{for ( ; __first1 != __last1; ++__first1, ++__first2)__init = __binary_op1(__init, __binary_op2(*__first1, *__first2));return __init;
}
作用:
计算[firtst1,last1)和[first2, first2+(last1-first1))的一般内积。
通过二元仿函数我们可以取代 operator* 和 operator+
例子:
//请使用比较完整支持c++11特性的编译器进行编译
#include <numeric>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;int main()
{int ia[5] = { 1,2,3,4,5 };vector<int> iv(ia, ia + 5);int init = 10;//计算内积// 1 2 3 4 5// * * * * *// 1 2 3 4 5// -----------------
// init+ 1+ 4+ 9+ 16+25 = 10+55 =65cout << inner_product(iv.begin(), iv.end(), iv.begin(), init) << endl;// 1 2 3 // * * * // 3 4 5 // -----------------
// init+ 3+ 8+ 15 = 10+26 =36cout << inner_product(iv.begin(), iv.begin() + 3, iv.begin() + 2, init) << endl;//利用仿函数代替operator* 和 operator+// 1 2 3 4 5// + + + + +// 1 2 3 4 5// -----------------
// init- 1- 4- 9- 16-25 = -20cout << inner_product(iv.begin(), iv.end(), iv.begin(), 10, minus<int>(), plus<int>()) << endl;return 0;
}
注意:
必须提供一个初始值init,这么做的原因之一是当[first1,last1)为空时,任然可以获得一个明确的值
在使用二元仿函数时,所有运算行为的顺序都有明确的设定
- 注意binary_op1 和 binary_op2 使用时的区别
STL之inner_product相关推荐
- C++ : STL常用算法: inner_product , sort ,itoa
目录 1.std::count 2.std::inner_product 3.atoi 4.itoa 5 is_sorted 6 sort 7. fill 8 mismatch 1.std::cou ...
- STL 之accumulate,adjacent_difference,inner_product,partial_sum
accumulate,adjacent_difference,inner_product,partial_sum 这些算法都是数字算法,因此只能操作数字类型的数据. 头文件 #include < ...
- STL源码剖析 数值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa
//版本1 template <class InputIterator,class T> T accumulate(InputIterator first,InputIterator la ...
- C++ STL inner_product函数的使用方法
//演示inner_product的各种正确打开方式 #include <iostream> #include <list> #include <numeric> ...
- C++拾取——使用stl标准库实现排序算法及评测
今天看了一篇文章,讲各种语言的优势和劣势.其中一个观点:haskell非常适合写算法,因为使用者不用去关心具体的计算机实现,而只要关注于操作语义.这让它在专心研究算法的人中非常受欢迎.所以很多时候,语 ...
- Effective STL 50条有效使用STL的经验笔记
Scott Meyers大师Effective三部曲:Effective C++.More Effective C++.Effective STL,这三本书出版已很多年,后来又出版了Effective ...
- 【C++】Effective STL:50条有效使用STL的经验
第一条:慎重选择容器类型 1.C++容器:先混个眼熟 序列容器:array.vector.string.deque.list.forward_list 有序关联容器:set.map.multiset. ...
- 《Effective STL》学习笔记(第四部分)
6.仿函数.仿函数类.函数等 函数和类似函数的对象--仿函数--遍布STL.关联容器使用它们来使元素保持有 序:find_if使用它们来控制它们的行为:如果缺少它们,那么比如for_each和tran ...
- STL算法algorithm,
2019独角兽企业重金招聘Python工程师标准>>> STL算法部分主要由头文件<algorithm>,<numeric>,<functional&g ...
最新文章
- 关于移动端rem适配
- java+@api_Java 常用的api
- 计算机考研计组简答题复习-本篇长期更新
- QT实现有纹理和照明的简单立方体。
- 老男孩教育参观云计算公司
- 微型计算机储存信息的基本单位是什么,16.磁盘存储器存、取信息的最基本单位是...
- 百练 Let it Bead
- python按键退出循环_python – 按退出键退出循环
- 浪潮n系列服务器指示灯_中国服务器市场,浪潮跑出,联想和华为出现衰退
- (6)GPS坐标与UTM坐标的转换
- 如何修改hosts文件?几种修改hosts文件的方法
- adb重启或关机手机命令
- 苹果绕过ID_三分钟教你绕过苹果ID锁,救你的板砖。-海绵宝宝的蟹黄堡
- 基于氚云平台的应用开发学习(一)
- 前端工程化配置-husky + eslint + lint-staged
- 网络层HTPPS和HTTP的概念与区别
- Linux中文件的可读,可写,可执行权限的解读以及chmod,chown,chgrp 命令的用法
- Java版支付宝手机网站支付
- ionic4-ts 字符串中获取汉字与去掉汉字
- 微软认证考试心得:微软认证考试的几种形式
热门文章
- mysql并行加载机制_Mysql表引擎优化
- wireshark找不到接口_下水管漏水,维修师傅看一眼就收了200,自己换其实不到10块...
- 卷积核权值初始化_Pytorch卷积层手动初始化权值的实例
- 惠普暗影精灵3清灰_如何评价惠普笔记本这几年的表现?尤其是暗影精灵系列。...
- android的交互方式,Android与js的交互方式
- rtsp转rtmp服务linux,ubuntu安装流媒体服务器(nginx+rtmp,rtsp转rtmp,rtsp转m3u8)
- python图形代码怎么写_【Python3-API】定制化图像接口示例代码
- tp5 queue.php,tp5(think-queue)消息队列+supervisor进程管理实现队列常驻进程
- 65279 php,php头部#65279;去除bom执行文件
- Python -- abc module