如何找到向量元素的乘积?
How to find the product of vector elements?
我有以下矢量值:[2, 3, 7]
。
我想输出向量的乘积,如2*3*7 = 42
。
我为它写了一些代码,但它似乎不起作用。我是 C++ 的新手,所以我不确定如何在给定任何大小的任何数字向量的情况下获取向量中值的乘积。
#include <bits/stdc++.h>
int main()
{
int n;
cin >> n;
vector<int> vec;
while (n--)
{
int temp;
cin >> temp;
vec.push_back(temp);
}
int total = 1;
total *= vec;
cout << vec << endl;
return 0;
}
尝试将向量内的每个值相乘。
for(std::size_t i=0; i<vec.size(); i++) {
total *= vec[i];
}
如果您想获得 任何大小的任何数值向量的乘积,这里有一个函数可以在模板的帮助下处理任何数值类型的向量:
template <class any>
long double vectorProduct(vector<any> vec) {
long double total = 1;
for(any num : vec) {
total *= num;
}
return total;
}
用法:
cout << vectorProduct(vec) << endl;
使用std::accumulate
,可以做到
#include <numeric> // std::accumulate
#include <functional> // std::multiplies
const auto total = std::accumulate(vec.cbegin(), vec.cend(), 1, std::multiplies<int>{});
通过包装到模板函数中,代码会更通用
template<typename Type>
auto product(const std::vector<Type>& vec, Type init)
{
return std::accumulate(vec.cbegin(), vec.cend(), init, std::multiplies<Type>{});
}
并用
调用它
const auto total = product(vec, /*value to be initialized/ started with*/);
对于 std,您可以使用 std::accumulate
:
int product(const std::vector<int>& v)
{
return std::accumulate(v.begin(), v.end(), 1, std::multiplies<>{});
}
以下是我将为您的示例做的事情:
#include <iostream>
int main ()
{
int n;
std::cin >> n;
int total = 1;
while(n--) {
int temp;
std::cin >> temp;
total *= temp;
}
std::cout << "Total: " << total << std::endl;
return 0;
}
我的解决方案使用 std::accumulate
with the operator std::multiplies
通过将所有元素相乘来累加它们。只需修改您的代码,最终结果将是:
#include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
std::vector<int> vec;
while(n--) {
int temp;
std::cin >> temp;
vec.push_back(temp);
}
int result = std::accumulate(std::begin(vec), std::end(vec), 1, std::multiplies<int>());
std::cout << result << std::endl;
return 0;
}
你根本没有处理向量,也输出了向量而不是总结果。
我有以下矢量值:[2, 3, 7]
。
我想输出向量的乘积,如2*3*7 = 42
。
我为它写了一些代码,但它似乎不起作用。我是 C++ 的新手,所以我不确定如何在给定任何大小的任何数字向量的情况下获取向量中值的乘积。
#include <bits/stdc++.h>
int main()
{
int n;
cin >> n;
vector<int> vec;
while (n--)
{
int temp;
cin >> temp;
vec.push_back(temp);
}
int total = 1;
total *= vec;
cout << vec << endl;
return 0;
}
尝试将向量内的每个值相乘。
for(std::size_t i=0; i<vec.size(); i++) {
total *= vec[i];
}
如果您想获得 任何大小的任何数值向量的乘积,这里有一个函数可以在模板的帮助下处理任何数值类型的向量:
template <class any>
long double vectorProduct(vector<any> vec) {
long double total = 1;
for(any num : vec) {
total *= num;
}
return total;
}
用法:
cout << vectorProduct(vec) << endl;
使用std::accumulate
,可以做到
#include <numeric> // std::accumulate
#include <functional> // std::multiplies
const auto total = std::accumulate(vec.cbegin(), vec.cend(), 1, std::multiplies<int>{});
通过包装到模板函数中,代码会更通用
template<typename Type>
auto product(const std::vector<Type>& vec, Type init)
{
return std::accumulate(vec.cbegin(), vec.cend(), init, std::multiplies<Type>{});
}
并用
调用它const auto total = product(vec, /*value to be initialized/ started with*/);
对于 std,您可以使用 std::accumulate
:
int product(const std::vector<int>& v)
{
return std::accumulate(v.begin(), v.end(), 1, std::multiplies<>{});
}
以下是我将为您的示例做的事情:
#include <iostream>
int main ()
{
int n;
std::cin >> n;
int total = 1;
while(n--) {
int temp;
std::cin >> temp;
total *= temp;
}
std::cout << "Total: " << total << std::endl;
return 0;
}
我的解决方案使用 std::accumulate
with the operator std::multiplies
通过将所有元素相乘来累加它们。只需修改您的代码,最终结果将是:
#include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
std::vector<int> vec;
while(n--) {
int temp;
std::cin >> temp;
vec.push_back(temp);
}
int result = std::accumulate(std::begin(vec), std::end(vec), 1, std::multiplies<int>());
std::cout << result << std::endl;
return 0;
}
你根本没有处理向量,也输出了向量而不是总结果。