当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::adjacent_difference用法及代码示例


计算范围的相邻差
分配给从结果开始的范围内的每个元素,其在[first,last]范围内的对应元素与该元素之前的元素之间的差(除* result外,其分配为* first)。

如果x表示[first,last]中的元素,而y表示结果中的元素,则ys可计算为:

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

1.使用默认版本:语法:模板:


OutputIterator adjacent_difference (InputIterator first,
                                    InputIterator last,
                                    OutputIterator result);
参数:

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]. 

返回类型:
An iterator pointing to past the last element of the destination
sequence where resulting elements have been stored.

// CPP program to illustrate 
// std::adjacent_difference 
  
#include <iostream> // std::cout 
#include <numeric> // std::adjacent_difference 
  
// Driver code 
int main() 
{ 
    int val[] = { 1, 2, 3, 5, 9, 11, 12 }; 
    int n = sizeof(val) / sizeof(val[0]); 
    int result[7]; 
  
    // Array contains 
    std::cout << "Array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << val[i]; 
    std::cout << "\n"; 
  
    // Using default std::adjacent_difference 
    std::adjacent_difference(val, val + 7, result); 
    std::cout << "Using default adjacent_difference:"; 
    for (int i = 1; i < n; i++) 
        std::cout << result[i] << ' '; 
    std::cout << '\n'; 
  
    return 0; 
}

输出:

Array contains:1 2 3 5 9 11 12
Using default adjacent_difference:1 1 2 4 2 1

2.使用自定义版本,以函数作为补偿语法:模板:

OutputIterator adjacent_difference (InputIterator first,
                                    InputIterator last,
                                    OutputIterator result,
                                    BinaryOperation binary_op);

参数:

first, last, result are same as above.

binary_op
Binary operation taking as arguments two elements of the type
pointed by InputIterator, and returning the result of the
replacement for the difference operation.
This can either be a function pointer or a function object. 

返回类型:
An iterator pointing to past the last element of the destination
sequence where resulting elements have been stored.

通过在自定义函数中将运算符更改为任何二进制运算符,我们可以更改在STL函数上应用的运算。此处执行相邻元素的总和。

// CPP program to illustrate 
// std::adjacent_difference 
  
#include <iostream> // std::cout 
#include <numeric> // std::adjacent_difference 
  
int comp(int x, int y) 
{ 
    return x + y; 
} 
  
// Driver code 
int main() 
{ 
    int val[] = { 1, 2, 3, 5, 9, 11, 12 }; 
    int n = sizeof(val) / sizeof(val[0]); 
    int result[7]; 
  
    // Array contains 
    std::cout << "Array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << val[i]; 
    std::cout << "\n"; 
  
    // std::adjacent_difference using custom function 
    std::adjacent_difference(val, val + 7, result, comp); 
  
    std::cout << "Using custom function:"; 
    for (int i = 0; i < n; i++) 
        std::cout << result[i] << ' '; 
    std::cout << '\n'; 
  
    return 0; 
}

输出:

Array contains:1 2 3 5 9 11 12
Using custom function:1 3 5 8 14 20 23 

实际应用:在所述范围的相邻元素之间执行任何二进制运算(范围的第一个元素除外)。

1.查找数组中相邻元素的乘积。
例如,数组包含:2 4 5 6
结果是:2 8 20 30
说明-第一个元素保持原样。然后第二个元素将是第一个元素*第二个元素,然后第三个元素将是第二个元素*第三个元素,依此类推。

// CPP program to illustrate 
// std::adjacent_difference 
  
#include <iostream> // std::cout 
#include <numeric> // std::adjacent_difference 
  
int comp(int x, int y) 
{ 
    return x * y; 
} 
  
// Driver code 
int main() 
{ 
    int val[] = { 5, 7, 4, 8, 2 }; 
    int n = sizeof(val) / sizeof(val[0]); 
    int result[n]; 
  
    // Array contains 
    std::cout << "Array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << val[i]; 
    std::cout << "\n"; 
  
    // Using custom std::adjacent_difference 
    std::adjacent_difference(val, val + 7, result, comp); 
    std::cout << "Result contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << ' ' << result[i]; 
    std::cout << '\n'; 
  
    return 0; 
}

输出:

Array contains:5 7 4 8 2
Result contains:5 35 28 32 16


相关用法


注:本文由纯净天空筛选整理自 std::adjacent_difference in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。