當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ std::inner_product用法及代碼示例

計算範圍的累積內積
返回以從first1和first2開始的兩個範圍的元素形成的對的內積對init進行累加的結果。

可以使用參數binary_op1和binary_op2覆蓋這兩個默認操作(以將對乘的結果相加)。

1.使用默認的inner_product:語法:


Template:
T inner_product (InputIterator1 first1, InputIterator1 last1,
                 InputIterator2 first2, T init);

參數:

first1, last1
Input iterators to the initial and final positions in the first
sequence.

first2
Input iterator to the initial position in the second sequence.
The range starts at first2 and has as many elements as the range
above [first1, last1].

init
Initial value for the accumulator.

Neither operations shall modify any of the elements passed as
its arguments.

返回類型:
The result of accumulating init and the products of all the pairs
of elements in the ranges starting at first1 and first2.
// CPP program to illustrate 
// std::inner_product 
#include <iostream> // std::cout 
#include <functional> // std::minus, std::divides 
#include <numeric> // std::inner_product 
  
// Driver code 
int main() 
{ 
    // The value which is added after 
    // finding inner_product b/w elements 
    int init = 100; 
    int series1[] = { 10, 20, 30 }; 
    int series2[] = { 1, 2, 3 }; 
    int n = sizeof(series1) / sizeof(series1[0]); 
  
    // Elements in series1 
    std::cout << "First array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << series1[i]; 
    std::cout << "\n"; 
  
    // Elements in series2 
    std::cout << "Second array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << series2[i]; 
    std::cout << "\n\n"; 
  
    std::cout << "Using default inner_product:"; 
    std::cout << std::inner_product(series1, series1 + n, series2, init); 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:

First array contains:10 20 30
Second array contains:1 2 3

Using default inner_product:240

2.使用函數操作:語法:

Template:
T inner_product (InputIterator1 first1, InputIterator1 last1,
                 InputIterator2 first2, T init,
                 BinaryOperation1 binary_op1,
                 BinaryOperation2 binary_op2);

參數:

first1, last1, first2, init are same as above.

binary_op1
Binary operation taking two elements of type T as arguments, and
returning the result of an accumulation operation.
This can either be a function pointer or a function object.

binary_op2
Binary operation taking two elements of type T as arguments, and
returning the result of the inner product operation.
This can either be a function pointer or a function object.

Here binary_op1 and binary_op2 are functional operation.

Neither operations shall modify any of the elements passed as
its arguments.

返回類型:
The result of accumulating init and the products of all the pairs
of elements in the ranges starting at first1 and first2.
// CPP program to illustrate 
// std::inner_product 
#include <iostream> // std::cout 
#include <functional> // std::minus, std::divides 
#include <numeric> // std::inner_product 
  
// Driver code 
int main() 
{ 
    // The value which is added after 
    // finding inner_product b/w elements 
    int init = 100; 
    int series1[] = { 10, 20, 30 }; 
    int series2[] = { 1, 2, 3 }; 
    int n = sizeof(series1) / sizeof(series1[0]); 
  
    // Elements in series1 
    std::cout << "First array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << series1[i]; 
    std::cout << "\n"; 
  
    // Elements in series2 
    std::cout << "Second array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << series2[i]; 
    std::cout << "\n\n"; 
  
    std::cout << "Using functional operations:"; 
    // std::minus returns the difference b/w 
    // each elements of both array 
    // std::divides return the quotient of 
    // each elements of both array after performing 
    // divide operation 
    // The operations is performed b/w number of same index 
    // of both array 
    std::cout << std::inner_product(series1, series1 + n, series2, init, 
                                    std::minus<int>(), std::divides<int>()); 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:

First array contains:10 20 30
Second array contains:1 2 3

Using functional operations:70

3.使用自定義函數:語法:

Template:
T inner_product (InputIterator1 first1, InputIterator1 last1,
                 InputIterator2 first2, T init,
                 BinaryOperation1 binary_op1,
                 BinaryOperation2 binary_op2);

參數:

first1, last1, first2, init are same as above.

binary_op1
Binary operation taking two elements of type T as arguments, and
returning the result of an accumulation operation.
This can either be a function pointer or a function object.

binary_op2
Binary operation taking two elements of type T as arguments, and
returning the result of the inner product operation.
This can either be a function pointer or a function object.

Neither operations shall modify any of the elements passed as
its arguments.

返回類型:
The result of accumulating init and the products of all the pairs
of elements in the ranges starting at first1 and first2.
// CPP program to illustrate 
// std::inner_product 
#include <iostream> // std::cout 
#include <functional> // std::minus, std::divides 
#include <numeric> // std::inner_product 
  
// Custom funcitons 
int myaccumulator(int x, int y) 
{ 
    return x - y; 
} 
int myproduct(int x, int y) 
{ 
    return x + y; 
} 
  
// Driver code 
int main() 
{ 
    // The value which is added after 
    // finding inner_product b/w elements 
    int init = 100; 
    int series1[] = { 10, 20, 30 }; 
    int series2[] = { 1, 2, 3 }; 
    int n = sizeof(series1) / sizeof(series1[0]); 
  
    // Elements in series1 
    std::cout << "First array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << series1[i]; 
    std::cout << "\n"; 
  
    // Elements in series2 
    std::cout << "Second array contains:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << series2[i]; 
    std::cout << "\n\n"; 
  
    std::cout << "Using custom functions:"; 
    std::cout << std::inner_product(series1, series1 + 3, series2, init, 
                                    myaccumulator, myproduct); 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:

First array contains:10 20 30
Second array contains:1 2 3

Using custom functions:34

注意:
通過使用函數值和自定義函數,我們可以通過在此STL函數中更改運算符(或使用其他函數值)來執行操作。

可能的應用:它返回以從first1和first2開始的兩個範圍的元素形成的對的內積對init進行累加的結果。

1.可用於查找兩個數組的第i個索引的乘積和。
例如:
數組1:1 2 3 4
數組2:10 20 30 40

產品總數:300

說明:1 * 10 + 2 * 20 + 3 * 30 + 4 * 40 = 300

// CPP program to illustrate 
// std::inner_product 
#include <iostream> // std::cout 
#include <functional> // std::minus, std::divides 
#include <numeric> // std::inner_product 
  
// Custom funcitons 
int myaccumulator(int x, int y) 
{ 
    return x + y; 
} 
int myproduct(int x, int y) 
{ 
    return x * y; 
} 
  
// Driver code 
int main() 
{ 
    // The value which is added after 
    // finding inner_product b/w elements 
    int init = 0; 
    int series1[] = { 1, 2, 3, 4 }; 
    int series2[] = { 10, 20, 30, 40 }; 
    int n = sizeof(series1) / sizeof(series1[0]); 
  
    // Elements in series1 
    std::cout << "Array 1:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << series1[i]; 
    std::cout << "\n"; 
  
    // Elements in series2 
    std::cout << "Array 2:"; 
    for (int i = 0; i < n; i++) 
        std::cout << " " << series2[i]; 
    std::cout << "\n\n"; 
  
    std::cout << "Sum of products:"; 
    std::cout << std::inner_product(series1, series1 + n, series2, init, 
                                    myaccumulator, myproduct); 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:

Array 1:1 2 3 4
Array 2:10 20 30 40

Sum of products:300

我們還可以通過更改運算符來找到乘積之差,或的總和,或除的差等等。



相關用法


注:本文由純淨天空篩選整理自 std::inner_product in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。