STL之adjacent_difference
需要的头文件:
numeric
源码:
//版本1
template <class _InputIterator, class _OutputIterator>
_OutputIterator
adjacent_difference(_InputIterator __first,_InputIterator __last, _OutputIterator __result)
{if (__first == __last) return __result;*__result = *__first;return __adjacent_difference(__first, __last, __result,__VALUE_TYPE(__first));
}
template <class _InputIterator, class _OutputIterator, class _Tp>
_OutputIterator
__adjacent_difference(_InputIterator __first, _InputIterator __last,_OutputIterator __result, _Tp*)
{_Tp __value = *__first;while (++__first != __last) {_Tp __tmp = *__first;*++__result = __tmp - __value;__value = __tmp;}return ++__result;
}//版本2
template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
_OutputIterator
adjacent_difference(_InputIterator __first, _InputIterator __last,_OutputIterator __result, _BinaryOperation __binary_op)
{if (__first == __last) return __result;*__result = *__first;return __adjacent_difference(__first, __last, __result,__VALUE_TYPE(__first),__binary_op);
}
template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOperation>
_OutputIterator
__adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp*,_BinaryOperation __binary_op) {_Tp __value = *__first;while (++__first != __last) {_Tp __tmp = *__first;*++__result = __binary_op(__tmp, __value);__value = __tmp;}return ++__result;
}
作用:
用来计算[first,last)中相邻元素的差额。其过程为:
存储第一元素之值,然后存储后继两两相邻元素之差值
通过二元仿函数,我们可以将它一般化为:
存储第一元素之值,然后存储后继两两相邻元素之运算值
例子:
#include <numeric>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;int main()
{int ia[5] = { 1,2,3,4,5 };vector<int> iv(ia, ia + 5);//作为输出ostream_iterator<int> oiter(cout, " ");//输出iv元素间的差值adjacent_difference(iv.begin(), iv.end(), oiter); //1 1 1 1 1cout << endl;//输出iv元素之间的两两相加的值adjacent_difference(iv.begin(), iv.end(), oiter, [](int a, int b) {return a + b;});//1 3 5 7 9cout << endl;}
注意:
首先会存储第一元素之值
二元仿函数中,参数有先后。第一个参数代表前面的元素,第二个参数代表后面的元素
STL之adjacent_difference相关推荐
- 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++11标准模板(STL)- 算法(std::adjacent_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- 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 ...
- C++ STL 总结
为什么80%的码农都做不了架构师?>>> 一.STL的六大组件 1.容器(Container),是一种数据结构,如list,vector,和deques ,以模板类的方法提供. ...
- Effective STL 条款30
http://blog.csdn.net/amin2001/article/details/8063 STL容器在被添加时(通过insert.push_front.push_back等)自动扩展它们自 ...
最新文章
- python带我起飞 豆瓣评分_你听过后觉得爽到飞起的电音是哪一首?
- 命名实参和可选实参(C#)
- 如何轻松愉快的理解条件随机场(CRF)
- TemplatePart用法说明
- linux shell 解析文本文件,linux Shell 全解析
- 3D渲染和动画制作KeyShot Pro for mac
- 数字地球与计算机技术联系,数字地球与地球空间信息科学的关系
- SAP WM Stock Removal Strategy - StringentFIFO 在仓库号级别下的先进先出
- ns3--入门基础概念
- 10的几次方 用计算机计算公式,在excel中如何计算10的几次方呢
- 有关java的几个日期类的转换
- 从jquery.tip理解jquery插件开发
- win10巨帧数据包在哪里设置_电脑和路由器mtu值怎样设置才网速最快
- redis数据库(一)
- Python3 心路历程
- System.out::println; 是什么鬼???
- oracle中怎样判断数据是否为空,为空赋值,不为空也赋值
- MD5骨骼动画模型加载
- Java中Lambada表达式使用方法
- 《途客圈创业记:不疯魔,不成活》一一2.3 早期产品
热门文章
- php mysql 地图 矩形_PHP+Mysql+jQuery中国地图区域数据统计实例讲解
- 跑步记录日期怎么改_快捷增加历史记录-鲨鱼记账App功能优化
- 石头剪子布蜥蜴史波克python_C++字符串——石头剪刀布
- 8_python基础—高级变量类型(字符串、列表、元组、字典、集合)
- python制作的游戏如何转化为swf_如何从python生成swf格式的幻灯片?
- python清除实例化类_在Python中,如何尝试(和排除)类的实例化?
- vue中集合取第一个_快速学习Vue框架(知识点集合)
- [渲染层错误] multipolyline.styles: 样式id_【译】关于 SPA,你需要掌握的 4 层
- TensorFlow保存和恢复模型的方法总结
- 导入maven项目报错无法运行