C++ std::multiplies实现无视类型执行乘法

std::multiplies是乘法的二元函数对象。

常被用于std::transform或者std::accumulate等的运算算子。
例子一.实现两个数组元素的相乘

// C++ program to illustrate std::multiplies 
// by multiplying the respective elements of 2 arrays 
#include <iostream> // std::cout 
#include <functional> // std::multiplies 
#include <algorithm> // std::transform 
  
int main() 
{ 
    // First array 
    int first[] = { 1, 2, 3, 4, 5 }; 
  
    // Second array 
    int second[] = { 10, 20, 30, 40, 50 }; 
  
    // Result array 
    int results[5]; 
  
    // std::transform applies std::multiplies to the whole array 
    std::transform(first, first + 5, second, results, std::multiplies<int>()); 
  
    // Printing the result array 
    for (int i = 0; i < 5; i++) 
        std::cout << results[i] << " "; 
  
    return 0; 
}

//output:10 40 90 160 250

例子二.实现多个数字的累乘

#include <bits/stdc++.h> 
  
int main() 
{ 
    // Array with elements to be multiplying 
    int arr[] = { 10, 20, 30 }; 
  
    // size of array 
    int size = sizeof(arr) / sizeof(arr[0]); 
  
    // Variable with which array is to be multiplied 
    int num = 10; 
  
    // Variable to store result 
    int result; 
  
    // using std::accumulate to perform multiplication on array with num 
    // using std::multiplies 
    result = std::accumulate(arr, arr + size, num, std::multiplies<int>()); 
  
    // Printing the result 
    std::cout << "The result of 10 * 10 * 20 * 30 is " << result; 
  
    return 0; 
}
//output:The result of 10 * 10 * 20 * 30 is 60000

例子三.两个vector数组元素相乘

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

int main()
{
    std::vector<int> v1 = {10, 20, 30, 40, 50};
    std::vector<int> v2 = { 1, 2, 3, 4, 5 };

    std::vector<int> result(5);
    std::transform(v1.begin(), v1.end(), v2.begin(), result.begin(), std::multiplies<int>());

    for (int i : result) {
        std::cout << i << "\t";
    }
    std::cout << std::endl;

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`std::accumulate` 是C++ STL 中的一个算法函数,它的作用是计算给定范围内的元素的累加和。它的函数原型如下: ```cpp template <class InputIterator, class T> T accumulate (InputIterator first, InputIterator last, T init); ``` 其中: - `first` 和 `last` 是输入范围的迭代器,表示要计算累加和的元素范围。 - `init` 是累加和的初始值。 `std::accumulate` 的工作方式是将累加和的初始值(即 `init`)加上范围内的每个元素,然后返回最终的累加和。 例如,以下代码演示了如何使用 `std::accumulate` 计算 `std::vector<int>` 中的元素之和: ```cpp #include <iostream> #include <numeric> #include <vector> int main() { std::vector<int> v {1, 2, 3, 4, 5}; int sum = std::accumulate(v.begin(), v.end(), 0); std::cout << "The sum is " << sum << std::endl; return 0; } ``` 输出结果为: ``` The sum is 15 ``` 在这个例子中,`v.begin()` 和 `v.end()` 分别表示 `std::vector<int>` 的第一个和最后一个元素的迭代器。`0` 是累加和的初始值。`std::accumulate` 计算了 `1 + 2 + 3 + 4 + 5` 的结果,即 `15`,并将其存储在 `sum` 变量中。 除了计算元素的累加和,`std::accumulate` 还可以用于计算其他类型的累积操作,例如乘积、最大值、最小值等等。这可以通过提供不同的二元操作函数来实现,例如 `std::multiplies` 用于计算元素的乘积,`std::max` 和 `std::min` 分别用于计算元素的最大值和最小值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值