STL-----------------adjacent_difference

 
如果要想真正学好STL 提供网站  http://www.cplusplus.com/reference/algorithm/count/
 
 
template <class InputIterator, class OutputIterator>
   OutputIterator adjacent_difference ( InputIterator first, InputIterator last,
                                        OutputIterator result );

template <class InputIterator, class OutputIterator, class BinaryOperation>
   OutputIterator adjacent_difference ( InputIterator first, InputIterator last,
                                        OutputIterator result, BinaryOperation binary_op );
Compute adjacent difference of range
Assigns to every element in the range starting at result the difference between its corresponding elements in the range [first,last) and the one preceding it (except for *result which is assigned *first).

If x represents an element in [first,last) and y represents an element in result, the ys can be calculated as:

y0 = x0
y1 = x1 - x0
y2 = x2 - x1
y3 = x3 - x2
y4 = x4 - x3
... ... ...

The default operation is to calculate the difference, but some other operation can be specified as binary_op instead.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class InputIterator, class OutputIterator>
   OutputIterator adjacent_difference ( InputIterator first, InputIterator last,
                                        OutputIterator result )
{
  iterator_traits<InputIterator>::value_type val,prev;
  *result++ = prev = *first++;
  while (first!=last) {
    val = *first++;
    *result++ = val - prev;  // or: *result++ = binary_op(val,prev) for binary_op version
    prev = val;
  }
  return result;
}


Parameters

first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
result
Output iterator to the initial position in the destination sequence where the differences are stored. The range starts at result and shall have a size large enough to contain as many elements as the range above ( [first,last)).
binary_op
Binary operation taking as arguments two elements of the type pointed by the InputIterator, and returning the result of the replacement for the difference operation. This can either be a pointer to a function or an object whose class overloads operator().

Return value

An iterator pointing to past the last element of the destination sequence where resulting elements have been stored.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// adjacent_difference example
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;

int myop (int x, int y) {return x+y;}

int main () {
  int val[] = {1,2,3,5,9,11,12};
  int result[7];

  adjacent_difference (val, val+7, result);
  cout << "using default adjacent_difference: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;

  adjacent_difference (val, val+7, result, multiplies<int>());
  cout << "using functional operation multiplies: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;

  adjacent_difference (val, val+7, result, myop);
  cout << "using custom function: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;
  return 0;
}



 

#include <iostream>
#include <functional>
#include <numeric>
using namespace std;

int myop (int x, int y) {return x+y;}

int main () {
  int val[] = {1,2,3,5,9,11,12};
  int result[7];

  adjacent_difference (val, val+7, result);
  cout << "using default adjacent_difference: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;

  adjacent_difference (val, val+7, result, multiplies<int>());
  cout << "using functional operation multiplies: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;

  adjacent_difference (val, val+7, result, myop);
  cout << "using custom function: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;
  return 0;
}

 

#include <iostream>
#include <functional>
#include <numeric>
using namespace std;

int myop (int x, int y) {return x+y;}

int main () {
  int val[] = {1,2,3,5,9,11,12};
  int result[7];

  adjacent_difference (val, val+7, result);
  cout << "using default adjacent_difference: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;

  adjacent_difference (val, val+7, result, multiplies<int>());
  cout << "using functional operation multiplies: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;

  adjacent_difference (val, val+7, result, myop);
  cout << "using custom function: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;
  return 0;
}

 

 

Output:


using default adjacent_difference: 1 1 1 2 4 2 1
using functional operation multiplies: 1 2 6 15 45 99 132
using custom function: 1 3 5 8 14 20 23

#include <iostream>
#include <functional>
#include <numeric>
using namespace std;

int myop (int x, int y) {return x+y;}

int main () {
  int val[] = {1,2,3,5,9,11,12};
  int result[7];

  adjacent_difference (val, val+7, result);
  cout << "using default adjacent_difference: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;

  adjacent_difference (val, val+7, result, multiplies<int>());
  cout << "using functional operation multiplies: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;

  adjacent_difference (val, val+7, result, myop);
  cout << "using custom function: ";
  for (int i=0; i<7; i++) cout << result[i] << ' ';
  cout << endl;
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值