📜  C++中的std :: adjacent_difference

📅  最后修改于: 2021-05-30 06:41:33             🧑  作者: Mango

计算范围的相邻差
分配给从结果开始的范围内的每个元素,其在[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,
                                    InputIteratorlast,
                                    OutputIterator 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]. 

Return Type :
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  // std::cout
#include  // 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);

Parameters :

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. 

Return Type :
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  // std::cout
#include  // 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  // std::cout
#include  // 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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”