首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::adjacent_difference

Defined in header <numeric>

?

?

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)

(since 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)

(since C++17)

计算范围中每个相邻元素对的第二个和第一个之间的差异,[first, last)并将它们写入从开始的范围d_first + 1。未修改的副本first写入d_first

1,3)首先,创建一个acc类型为InputIt值类型的累加器,用它初始化*first,并将结果赋值给*d_first。然后,对于每一个迭代i[first + 1, last),以便,创建一个对象val,其类型为InputIt的值类型,以初始化它*i,单位计算val - acc(过载(1))或op(val, acc)(过载(3)),将结果赋给*(d_first + (i - first))和移动从分配valacc

输入范围可能重叠,但不能相同:如果行为未定义 first==d_first

2,4)首先,创建一个对象,其类型为ForwardIt1的值类型,用它初始化*first,并将结果赋值给*d_first。然后对于每个din [1, last - first - 1],创建一个val类型为ForwardIt1的值类型的对象,使用*(first + d) - *(first + d - 1)(overload(2))或op(*(first + d), *(first + d - 1))(overload(4))初始化它,并将结果赋值给*(d_first + d)。这是根据执行的policy。如果std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>为true,则此重载仅参与重载解析。

如果输入范围以任何方式重叠,则行为未定义。

等效操作:

二次

代码语言:javascript
复制
*(d_first)   = *first;
*(d_first+1) = *(first+1) - *(first);
*(d_first+2) = *(first+2) - *(first+1);
*(d_first+3) = *(first+3) - *(first+2);
...

二次

op必须没有副作用。

(直到C ++ 11)

op不得使任何迭代器无效,包括结束迭代器,或修改所涉及范围的任何元素。

(自C ++ 11以来)

参数

第一,最后

-

元素的范围

d_first

-

目的地范围的开头

政策

-

要使用的执行策略。有关详细信息,请参阅执

-

将应用的二进制运算函数对象。函数的签名应该等同于以下内容:Ret fun(const Type1&a,const Type2&b); 签名不需要const&。类型Type1和Type2必须使得iterator_traits <InputIt> :: value_type类型的对象可以隐式转换为它们。类型Ret必须是这样的,可以取消引用类型为OutputIt的对象并为其指定Ret类型的值。

| 类型要求|

| -InputIt必须满足InputIterator的要求。InputIt的值类型必须是MoveAssignable,并且可以从* first |的类型构造

| -OutputIt必须满足OutputIterator的要求。acc(累计值)和val-acc或op(val,acc)的结果必须可写入OutputIt |

| -ForwardIt1,ForwardIt2必须满足ForwardIterator的要求。ForwardIt1的value_type必须是CopyConstructible,可以从表达式* first - * first或op(* first,* first)构造,并可赋值给ForwardIt2的value_type |

返回值

它通过最后写的元素传递给元素。

注记

如果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 - acc; acc = std :: move(val); } return ++ d_first; } |

第二版

模板<类Inputit,类Outputit,类二进制操作>OutputIt相邻[医]差%28---先输入,最后输入,输出-d[医]首先,BinaryOperationop%29{如果%28first==最后%29返回d[医]第一;tyUIDUILF类型名称STD::iterator[医]性状<InputIt>*价值[医]类型值[医]T值[医]t acc=%2A第一;%2A丁[医]First=acc;而%28++First%21=最后%29{value[医]t Val=%2A第一;%2A++d[医]首先=OP%28 val,acc%29;acc=std::Move%28 val%29;}back++d[医]第一;}

下面的代码将偶数序列转换为数字2的重复,并将序列转换为斐波那契数序列。

二次

代码语言:javascript
复制
?#include <numeric>
#include <vector>
#include <iostream>
#include <functional>
 
int main()
{
    std::vector<int> v{2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    std::adjacent_difference(v.begin(), v.end(), v.begin());
 
    for (auto n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
 
    v = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    std::adjacent_difference(v.begin(), v.end() - 1, v.begin() + 1, std::plus<int>());
 
    for (auto n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
}?

二次

产出:

二次

代码语言:javascript
复制
2 2 2 2 2 2 2 2 2 2
1 1 2 3 5 8 13 21 34 55

二次

另见

partial_sum

computes the partial sum of a range of elements (function template)

accumulate

sums up a range of elements (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com