使用accumulate计算数组double []的平均值的函数 [英] Function for calculating the mean of an array double[] using accumulate

查看:1643
本文介绍了使用accumulate计算数组double []的平均值的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它必须是最常见的功能,每个人都有一个代码片段在某个地方,但我实际上花了不少于1.5小时搜索它在SO以及其他C ++网站,并没有找到一个解决方案。



我想计算 double array [] 的平均值。我想将数组作为引用传递给函数。有数百万的例子,其中的平均值是在main()循环中计算,但我要找的是一个函数,我可以放在外部文件,并在任何时间后使用它。



到目前为止,这是我最新的版本,给出了编译错误:

  double mean_array [])
{
int count = sizeof(array)/ sizeof(array [0]);
double sum = accumulate(array,array + count,0);
return(double)sum / count;
}

编译错误是:


错误C3861:'accumulate':未找到标识符


修复这个功能?这个编译错误是什么意思?



如果我使用 std :: accumulate / code>),那么我得到以下错误:

 'accumulate' '
'accumulate':未找到标识符

为什么'accumulate' 'std'?



ps:我知道我可以做'sum + = array [i]'方式,而不是使用accumulate,但是我想了解这里发生了什么

解决方案

尝试添加

  #include< numeric> 

它会带来你正在寻找的'std :: accumulate' >

更进一步,你会有一个问题,找出数组中的元素数量。事实上,数组不能传递给函数,希望函数能够知道数组的大小。它会衰减到指针。因此,您的 count 计算将会出错。如果你想传递实际大小的指定数组,你必须使用一个模板函数。

  template< int N> 
double mean_array(double(& array)[N])
{
return std :: accumulate(array,array + N,0.0)
}


It must be the most common function for what everyone has a code snippet somewhere, but I have actually spent no less than 1.5 hour searching for it on SO as well as on other C++ sites and have not found a solution.

I would like to calculate the mean of a double array[] using a function. I would like to pass the array to the function as a reference. There are millions of examples where the mean is calculated in a main() loop, but what I am looking for is a function what I can put in an external file and use it any time later.

So far, here is my latest version, what gives a compile error:

double mean_array( double array[] )
{
    int count = sizeof( array ) / sizeof( array[0] );
    double sum = accumulate( array, array + count, 0 );
    return ( double ) sum / count;
}

The compile error is:

error C3861: 'accumulate': identifier not found

Can you tell me how to fix this function? What does that compile error mean?

If I use std::accumulate (over the already defined using namespace std), then I get the following error:

'accumulate' : is not a member of 'std'
'accumulate': identifier not found

Why is 'accumulate' not a member of 'std'?

p.s.: I know I can do 'sum += array[i]' way and not use accumulate, but I would like to understand what is happening here and how can I make my example work.

解决方案

Try to add

#include <numeric>

It will bring in the 'std::accumulate' function you're looking for.

Going further, you're gonna have a problem to find out the number of elements in your array. Indeed, an array cannot be passed to a function in the hope that the function will be able to know the size of the array. It will decay to a pointer. Therefore, your count calculation will be wrong. If you want to be able to pass an actual size specified array, you have to use a templated function.

template <int N>
double mean_array( double ( & array )[N] )
{
    return std::accumulate( array, array + N, 0.0) / (double)(N);
}

这篇关于使用accumulate计算数组double []的平均值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆