📜  C++ STL中的accumulate()和partial_sum():数字标头

📅  最后修改于: 2021-05-30 11:16:32             🧑  作者: Mango

此标头是C++ STL中数字库的一部分。本文介绍了数字标头中的一些有用功能,这些功能可以在竞争性编程中使用,以节省时间和精力。

我们通常使用线性运算找出特定范围内或整个数组中元素的总和,这需要将范围内的所有元素一一相加,并在每次迭代后将其存储到某个变量中。

积累()

此函数返回位于[first,last)与变量sum之间的范围内的所有值的总和。

  1. 语法1:
    accumulate(first, last, sum);
    first, last : first and last elements of range 
                  whose elements are to be added
    sum :  initial value of the sum
    
  2. 语法2:此函数返回位于[first,last)与变量sum之间的所有值的和。
    accumulate(first, last, sum, myfun); 
    myfun : a function for performing any 
            specific task. For example, we can
            find product of elements between
            first and last.
    
// C++ program to demonstrate working of accumulate()
#include  
#include      
using namespace std;
  
// User defined function
int myfun(int x, int y) 
{
    // for this example we have taken product 
    // of adjacent numbers
    return x * y ;
}
  
int main() 
{
    // Initialize sum = 1 
    int sum = 1;
    int a[] = {5 , 10 , 15} ;
      
    // Simple default accumulate function
    cout << "\nResult using accumulate: ";
    cout << accumulate(a , a+3 , sum);
      
    // Using accumulate function with
    // defined function
    cout << "\nResult using accumulate with"
             "user-defined function: ";
    cout << accumulate(a, a+3, sum, myfun);
     
    // Using accumulate function with
    // pre-defined function 
    cout << "\nResult using accumulate with "
            "pre-defined function: ";
    cout << accumulate(a, a+3, sum, std::minus());
      
    return 0;
}

输出:

Result using accumulate: 31
Result using accumulate with user-defined function: 750
Result using accumulate with pre-defined function: -29

一个示例问题:第k1个最小元素与第k2个最小元素之间的所有元素的总和

partial_sum()

该函数将数组对应元素的部分和分配给第二个数组的每个位置,返回位于[first,last)之间的所有值的部分和,并将其存储在另一个数组b中。
例如,如果x表示[first,last)中的元素,而y表示结果中的元素,则ys可计算为:

y0 = x0 
y1 = x0 + x1 
y2 = x0 + x1 + x2 
y3 = x0 + x1 + x2 + x3 
y4 = x0 + x1 + x2 + x3 + x4

句法 :

partial_sum(first, last, b);
partial_sum(first, last, b, myfun);
first, last : first and last element of range 
              whose elements are to be added
b : index of array where  corresponding partial 
    sum will be stored;
myfun : a user defined function for performing 
        any specific task
// C++ program to demonstrate working of accumulate()
#include  
#include      
using namespace std;
  
//user defined function
int myfun(int x, int y)
{
    // the sum of element is twice of its 
    // adjacent element
    return x + 2 * y;
}
  
int main () 
{
    int a[] = {1, 2, 3, 4, 5} ;
    int b[5];
      
    // Default function
    partial_sum(a , a+5 , b);
  
    cout << "Partial Sum - Using Default function: ";
    for (int i=0; i<5; i++)
        cout << b[i] << ' ';
    cout << '\n';
      
    // Using user defined function
    partial_sum(a , a+5 , b , myfun) ;
  
    cout << "Partial sum - Using user defined function: ";
    for (int i=0; i<5; i++)
        cout << b[i] << ' ';
    cout << '\n';
  
    return 0;
}

输出 :

Partial Sum - Using Default function: 1 3 6 10 15 
Partial sum - Using user defined function: 1 5 11 19 29 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”