📜  C++ STL中的数字标头|集合2(adjacent_difference(),inner_product()和iota())

📅  最后修改于: 2021-05-26 02:52:31             🧑  作者: Mango

C++ STL中的数字标头|套装1

neighbor_difference

此函数将一个数组的相应元素之间的差异分配给另一个数组。它返回[First,last)之间的所有值集相邻差

例如:如果a []表示提供的范围[first,last)中的元素,而b []表示结果。

b[0] = a[1] 
b[1] = a[1] – a[0] 
b[2] = a[2] – a[1] 
b[3] = a[3] – a[2] 
b[4] = a[4] – a[3] 
... ... ...

句法:

adjacent_difference(first, last, b);
adjacent_difference(first, last, b, myfun );
adjacent_difference(first, last, b, multiplies() ) ;

first, last : address of 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
multiplies():a pre defined function.
#include  
#include  // for multiplies function
#include    //for adjacent_difference
  
using namespace std;
  
int myfun (int x, int y) 
{
    return x+y;
}
  
int main () 
{
    int a[] = { 1, 2, 3, 4, 5, 6} ;
    int b[6];
      
    // using adjacent_difference function
    adjacent_difference (a, a+6, b);
    cout << "\nResult using adjacent_difference: ";
    for (int i=0; i<6; i++) 
        std::cout << b[i] << ' ' ;   
      
    // using adjacent_difference function
    // user defined function    
    adjacent_difference (a, a+6, b, myfun);
    cout << "\nResult using accumulate with user-"
             "defined function: ";
    for (int i=0; i<6; i++) 
        std::cout << b[i] << ' ';
      
    // using adjacent_difference with pre-defined function
    adjacent_difference (a, a+6, b, multiplies() ) ;
      
    cout << "\nResult using accumulate with pre-defined function: " ;
    for (int i=0; i<6; i++) 
        std::cout << b[i] << ' ';
      
    return 0;
}

输出:

Result using adjacent_difference: 1 1 1 1 1 1 
Result using accumulate with user-defined function: 1 3 5 7 9 11 
Result using accumulate with pre-defined function: 1 2 6 12 20 30 

内部产品

此函数返回将var与从first1和first2开始的两个范围的元素形成的对的内积相加的结果。

句法:

inner_product(first, last, b, var) ;
inner_product(a, a+3, b, var, fun, fun1) ;
inner_product(a , a+3, b, init, minus (), divides () );

first, last : address of first and last element of range whose elements are to be added
b: index of array where  corresponding partial sum will be stored;
fun, fun1: a user defined function for performing any specific task
minus(), divides()  : pre defined function.
#include   
#include  // for subtraction, std::divides
#include    // for inner_product
  
using namespace std;
  
int fun (int x, int y) 
{
    return x-y;
}
  
int fun1 (int x, int y) 
{
    return x+y;
}
  
int main () 
{
    int var = 200;
    int a[] = { 10, 15, 20} ;
    int b[] = { 1, 3, 5} ;
      
    cout << "\nResult using inner_product " ;    
  
    // inner_product with default method
    cout << inner_product(a, a+3, b, var ) ;
      
    // inner_product with pre-defined function
    cout << "\nResult using inner_product with pre-defined function: ";
    cout << inner_product(a, a+3, b, var, minus(), divides());
      
    // inner_product with user defined function
    cout << "\nResult using inner_product with user-defined function: ";
    cout << inner_product(a, a+3, b, var, fun, fun1);
  
    return 0;
}

输出:

Result using inner_product 355
Result using inner_product with pre-defined function: 181
Result using inner_product with user-defined function: 146

iota

该函数为数组[first,last)范围内的元素分配一个值,该值在每个步骤中以val ++递增。

句法 –

iota(first, last,val) ;

first, last : address of first and last element of range whose elements are to be added
val: initial value to store, the expression ++value must be well-formed
#include  
#include  
#include    // std::iota
using namespace std;
  
int main ( )
{
    int a[7];
      
    //using iota function to store 100, 101, 102,...    
    iota(a, a+7,100);
    cout << " a : " ;
    for (int& x: a) 
        cout << ' ' << x;
      
    return 0;
}

输出 :

a: 100 101 102 103 104 105 106 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”