C++11标准模板(STL)- 算法(std::adjacent_difference)
定义于头文件 <algorithm>
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last)
,其中 last
指代要查询或修改的最后元素的后一个元素。
计算范围内各相邻元素之间的差
std::adjacent_difference
template< class InputIt, class OutputIt >
OutputIt adjacent_difference( InputIt first, InputIt last, OutputIt d_first ); |
(1) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 adjacent_difference( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first ); |
(2) | (C++17 起) |
template< class InputIt, class OutputIt, class BinaryOperation >
OutputIt adjacent_difference( InputIt first, InputIt last, OutputIt d_first, BinaryOperation op ); |
(3) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryOperation >
ForwardIt2 adjacent_difference( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, BinaryOperation op ); |
(4) | (C++17 起) |
计算 [first, last)
范围中每对相邻元素的第二个和第一个的差,并写入它们到始于 d_first + 1
的范围。写入不修改的 *first
副本到 *d_first
。
1,3) 首先,创建类型为 InputIt
的 value_type 的积累器 acc
,以 *first 初始化,并赋值为 *d_first 的结果。然后,对于 [first + 1, last)
中按顺序的每个迭代器 i
,创建类型为 InputIt
的 value_type 的对象 val
,以 *i 初始化,计算 val - acc (C++20 前)val - std::move(acc) (C++20 起) (重载 (1) )或 op(val, acc) (C++20 前)op(val, std::move(acc)) (C++20 起) (重载 (3) ),赋值结果到 *(d_first + (i - first)) ,并从 val
移动赋值到 acc
。
first 可以等于 d_first 。
2,4) 进行 *d_first = *first; 。对每个 [1, last - first - 1]
中的 d
,赋 *(first + d) - *(first + d - 1) (重载 (2) )或 op(*(first + d), *(first + d - 1)) (重载 (4) )给 *(d_first + d) 。按照 policy
执行之。此重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 才参与重载决议。
若输入与输出范围以任何方式重叠则行为未定义。
等价操作:
*(d_first) = *first;
*(d_first+1) = *(first+1) - *(first);
*(d_first+2) = *(first+2) - *(first+1);
*(d_first+3) = *(first+3) - *(first+2);
...
|
(C++11 前) |
|
(C++11 起) |
参数
first, last | - | 元素范围 |
d_first | - | 目标范围的起始 |
policy | - | 所用的执行策略。细节见执行策略。 |
op | - |
被使用的二元函数对象。
该函数的签名应当等价于: Ret fun(const Type1 &a, const Type2 &b); 签名中并不需要有 const &。 |
类型要求 | ||
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。InputIt 的 value_type 必须可移动赋值 (MoveAssignable) 且可从 *first 的类型构造
|
||
- OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。acc (积累的值)和 val - acc 或 op(val, acc) (C++20 前)val - std::move(acc) 或 op(val, std::move(acc)) (C++20 起) 结果都必须可写入 OutputIt
|
||
- ForwardIt1, ForwardIt2 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。*first 、 *first - *first (对于 (2) )和 op(*first, *first) (对于 (4) )的结果必须可写入 ForwardIt2 。
|
返回值
指向最后被写入元素后一位置的迭代器。
注意
若 first == last
,则此函数无效果并且只会返回 d_first
。
复杂度
恰好应用 (last - first) - 1
次二元运算。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 若作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
为标准策略之一,则调用 std::terminate 。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 若算法无法分配内存,则抛出 std::bad_alloc 。
可能的实现
版本一
template<class InputIt, class OutputIt>
OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first)
{if (first == last) return d_first;typedef typename std::iterator_traits<InputIt>::value_type value_t;value_t acc = *first;*d_first = acc;while (++first != last) {value_t val = *first;*++d_first = val - std::move(acc); // C++20 起有 std::moveacc = std::move(val);}return ++d_first;
}
版本二
template<class InputIt, class OutputIt, class BinaryOperation>
OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op)
{if (first == last) return d_first;typedef typename std::iterator_traits<InputIt>::value_type value_t;value_t acc = *first;*d_first = acc;while (++first != last) {value_t val = *first;*++d_first = op(val, std::move(acc)); // C++20 起有 std::moveacc = std::move(val);}return ++d_first;
}
调用示例
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <iterator>
#include <time.h>using namespace std;struct Cell
{int x;int y;Cell &operator +=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator +(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator -(const Cell &cell){x -= cell.x;y -= cell.y;return *this;}Cell &operator *(const Cell &cell){x *= cell.x;y *= cell.y;return *this;}Cell &operator ++(){x += 1;y += 1;return *this;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}bool operator ==(const Cell &cell) const{return x == cell.x && y == cell.y;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}int main()
{std::mt19937 g{std::random_device{}()};srand((unsigned)time(NULL));;std::cout << std::boolalpha;std::function<Cell()> generate = [](){int n = std::rand() % 10 + 100;Cell cell{n, n};return cell;};// 初始化lCells1std::list<vector<Cell>> lCells1(6, vector<Cell>(5));//用从起始值开始连续递增的值填充一个范围for (vector<Cell> &vCells : lCells1){std::generate(vCells.begin(), vCells.end(), generate);}size_t index = 0;for (vector<Cell> &vCells : lCells1){std::cout << "lCells " << index << " ";std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;vector<Cell> aCells(vCells.size());//计算 [first, last) 范围中每对相邻元素的第二个和第一个的差std::adjacent_difference(vCells.begin(), vCells.end(), aCells.begin());std::cout << "adjacent_difference " ;std::copy(aCells.begin(), aCells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;index++;}std::cout << std::endl;std::cout << std::endl;std::cout << std::endl;auto BinaryOperation = [](const Cell & a, const Cell & b){Cell cell{a.x - b.x, a.y - b.y};return cell;};// 初始化lCells2std::list<vector<Cell>> lCells2(6, vector<Cell>(5));for (vector<Cell> &vCells : lCells2){std::generate(vCells.begin(), vCells.end(), generate);}index = 0;for (vector<Cell> &vCells : lCells2){std::cout << "lCells " << index << " ";std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;vector<Cell> aCells(vCells.size());//计算 [first, last) 范围中每对相邻元素的第二个和第一个的差std::adjacent_difference(vCells.begin(), vCells.end(), aCells.begin(), BinaryOperation);std::cout << "adjacent_difference " ;std::copy(aCells.begin(), aCells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;index++;}return 0;
}
输出
C++11标准模板(STL)- 算法(std::adjacent_difference)相关推荐
- 【跟学C++】C++STL标准模板库——算法详细整理(下)(Study18)
文章目录 1.简介 2.STL算法分类及常用函数 2.2.变序算法(二) 2.2.1 替换算法(2个) 2.2.2 排序算法(6个) 2.2.3 分区算法(4个) 2.2.4 可用于排序容器的算法(3 ...
- 【跟学C++】C++STL标准模板库——算法详细整理(中)(Study18)
文章目录 1.简介 2.STL算法分类及常用函数 2.1.变序算法(一) 2.2.1 初始化算法(2个) 2.2.2 修改算法(2个) 2.2.3 复制算法(6个) 2.2.4 删除算法(6个) 3. ...
- c++ 11标准模板(STL) std::vector (十一)
定义于头文件 <vector> template< class T, class Allocator = std::allocator<T> > class ...
- 【跟学C++】C++STL标准模板库——算法详细整理(上)(Study18)
文章目录 1.STL简介 2.STL算法分类及常用函数 2.1.非变序算法 2.1.1 计数算法(2个) 2.1.2 搜索算法(7个) 2.1.3 比较算法(2个) 3.总结 =========== ...
- C++11后的STL算法
文章目录 一.函数对象 二.预定义的函数对象 三.算法函数 1.自己实现foreach算法 2.自己实现的findif算法 3.自己实现bsort算法 一.函数对象 STL提供了很多处理容器的函数模板 ...
- C++11标准模板(STL)- 算法(std::set_symmetric_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法(std::nth_element)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法 - 集合操作(在已排序范围上)(std::set_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法 - 数值运算 (std::accumulate)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
最新文章
- 基础必备 | Python处理文件系统的10种方法
- AntiXSS - 支持Html同时防止XSS攻击
- php算法求出一个数可以被分解成多少个_面试时写不出排序算法?看这篇就够了(下)...
- swiper高度自适应_微信小程序之swiper轮播图片高度自适应
- Spring MVC文件上传下载实例
- 关于elasticsearch boostrap checks failed错误类型整理及解决方法
- Ugly Number II
- 如何将开源项目部分代码作为private放在github上?
- HTC Z710t解锁 获取root权限
- 【考研计算机网络】 冲刺笔记
- 吃豆豆--Java小游戏
- Learning to Count via Unbalanced Optimal Transport
- 【21世纪电脑入门】总目录——系统配置、包/库安装、问题修复
- 【保研复习】C语言保研机试笔记
- 【spark使用】4. Dataset转换算子使用
- SpringBoot+MangoDB查询操作(MongoTemplate)总结
- 13.1 数状数组 ——【小朋友排队】
- OTG 线与普通 USB 数据线的区别
- icode五级训练场函数入门1-9
- 信号【2】-理解signal和sigaction