cpp/algorithm/adjacent difference:修订间差异

来自cppreference.com
P12bot留言 | 贡献
TranslationBot留言 | 贡献
Translated from the English version using Google Translate
第1行: 第1行:
{{title|adjacent_difference}}
{{
语法:
title|adjacent_difference}}


<syntaxhighlight lang="cpp">
计算[, )相邻的差,并且将开始的。
    #include <numeric>
    output_iterator adjacent_difference
    ( input_iterator start,
      input_iterator end,
      output_iterator result );
     
    output_iterator adjacent_difference
    ( input_iterator start,
      input_iterator end,
      output_iterator result,
      BinaryFunction f );
</syntaxhighlight>
adjacent_difference()函数计算区间[start,end)相邻元素的差,并且将结果存储在从result开始的地方


(特别地,位于start处的元素将被复制到result;而且start + i与start + (i-1)差值将会被存储到result + i
的。


如果给定了二函数f,则f将被用作计算差值操作而不是-操作
的。


===参数===
===
===


todo


===返回值===
======


todo


===例子===
=
=
=
=
=
=


下面的例子显示了vector中相邻元素差值。
<syntaxhighlight lang="cpp">
#include <algorithm>
下面的的
#include <iostream>
#include <iterator>
#include <numeric>
<>
  #include <vector>
#include <>
#include <iostream>
#include <>
<>
   
<


int main()
{
{
std::v()v.()
  std::vector<int> v;
  for (int i = 3; i <= 10; ++i)
    v.push_back(i);


  // display elements of v
: v)
  std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::<
std::cout ;


  std::cout << std::endl;


  // display all differences between elements
  std::adjacent_difference(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
  return 0;
}
</syntaxhighlight>
当运行时,这个代码会显示如下的输出:
<syntaxhighlight lang="cpp">
3 4 5 6 7 8 9 10
3 1 1 1 1 1 1 1
</syntaxhighlight>
===复杂度===
adjacent_difference() 函数在[[cpp/complexity | 线性时间]]内完成。
===参见===
[[cpp/algorithm/accumulate | accumulate]], [[cpp/algorithm/count | count]], [[cpp/algorithm/inner_product | inner_product]], [[cpp/algorithm/partial_sum | partial_sum]]
[[en:cpp/algorithm/adjacent difference]]
[[ja:cpp/algorithm/adjacent difference]]
[[ja:cpp/algorithm/adjacent difference]]
[[ru:cpp/algorithm/adjacent difference]]
[[ru:cpp/algorithm/adjacent difference]]

2012年10月25日 (四) 20:00的版本

<metanoindex/>

 
 
算法库
受约束算法及范围上的算法 (C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作(在已划分范围上)
集合操作(在有序范围上)
归并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库

数值运算
(C++11)                       
在未初始化内存上的操作
 
在标头 <numeric> 定义
template< class InputIt, class OutputIt >
OutputIt adjacent_difference( InputIt first, InputIt last, 
                                    OutputIt d_first );
(1)
template< class InputIt, class OutputIt, class BinaryOperation >
OutputIt adjacent_difference( InputIt first, InputIt last, 
                                    OutputIt d_first,
                                    BinaryOperation op );
(2)
。计算所述第二和第一元素的范围内[first, last)的每个相邻对之间的差异,并且将它们写入到的范围内开始,d_first + 1。未修改的副本,first被写入d_first。第一个版本使用operator-来计算的差异,第二个版本使用给定的二进制功能的op.
原文:
Computes the differences between the second and the first of each adjacent pair of elements of the range [first, last) and writes them to the range beginning at d_first + 1. Unmodified copy of first is written to d_first. The first version uses operator- to calculate the differences, the second version uses the given binary function op.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
。相同的操作。
原文:
Equivalent operation:
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
*(d_first)   = *first;
*(d_first+1) = (*first+1) - *(first);
*(d_first+2) = (*first+2) - *(first+1);
*(d_first+3) = (*first+3) - *(first+2);
...

。参数。

Template:param list begin Template:param list item Template:param list item Template:param list op2 Template:param list hreq Template:param list req concept Template:param list req concept Template:param list end

===。 返回值。===

。它过去的最后一个元素的元素写的。
原文:
It to the element past the last element written.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。复杂性。

。究竟(last - first) - 1应用程序的二进制运算。
原文:
Exactly (last - first) - 1 applications of the binary operation
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。可能的实现。

版本一
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;
}
版本二
template<class InputIt, class T, 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, acc);
        acc = std::move(val);
    }
    return ++d_first;
}

。为例。

。下面的代码转换为偶数序列的重复数2与斐波那契数列的序列的序列转换.
原文:
The following code converts a sequence of even numbers to repetitions of the number 2 and converts a sequence of ones to a sequence of Fibonacci numbers.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

​#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';
}

输出:

2 2 2 2 2 2 2 2 2 2
1 1 2 3 5 8 13 21 34 55

。另请参阅。

Template:cpp/algorithm/dcl list partial sumTemplate:cpp/algorithm/dcl list accumulate