现在的位置: 首页 > 综合 > 正文

C++ STL adjacent_difference

2019年06月07日 ⁄ 综合 ⁄ 共 1068字 ⁄ 字号 评论关闭
difference (1)
template <class InputIterator, class OutputIterator>
   OutputIterator adjacent_difference (InputIterator first, InputIterator last,
                                       OutputIterator result);
custom (2)
template <class InputIterator, class OutputIterator, class BinaryOperation>
   OutputIterator adjacent_difference ( InputIterator first, InputIterator last,
                                        OutputIterator result, BinaryOperation binary_op );

adjacent_difference : 对于给定的序列x0,x1,...,x(n-1),计算序列中相邻两个元素的差序列x1-x0,x2-x1,...,x(n-1)-x(n-2)。该算法可以把结果序列保存在原序列中,也可以保存在另一个区间中。

1:除了比较出差值,还可以作给定的交互。

2:第一个元素是不变的放入结果中的。

3:adjacent_difference() 是数值算法,使用 <numeric> 头文件。

#include "algostuff.hpp"
#include <iterator>
#include <ostream>
#include <numeric>

using namespace std;

int main(){
deque<int> coll;
INSERT_ELEMENTS(coll,1,6);
PRINT_ELEMENTS(coll);
cout<<endl;

adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
cout<<endl;

adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),plus<int>());
cout<<endl;

adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),multiplies<int>());
cout<<endl;

return 0;
}

编译后输出:

 1 2 3 4 5 6 
1 1 1 1 1 1 
1 3 5 7 9 11 
1 2 6 12 20 30 

【上篇】
【下篇】

抱歉!评论已关闭.