使用accumulate计算数组double[]平均值的函数
它一定是最常见的函数,每个人在某处都有代码片段,但我实际上花了不少于 1.5 小时在 SO 以及其他 C++ 网站上搜索它,但还没有找到解决方案。
我想使用函数计算双数组[]
的平均值。我想将数组作为引用传递给函数。有数百万个例子,其中平均值是在 main() 循环中计算的,但我正在寻找的是一个函数,我可以将其放入外部文件中并在以后随时使用它。
到目前为止,这是我的最新版本,是什么给出了编译错误:
double mean_array( double array[] )
{
int count = sizeof( array ) / sizeof( array[0] );
double sum = accumulate( array, array + count, 0 );
return ( double ) sum / count;
}
编译错误是:
错误 C3861:“累积”:找不到标识符
您能告诉我如何修复此功能吗?那个编译错误是什么意思?
如果我使用 std::accumulate
(在已经定义的 using namespace std
上),则会收到以下错误:
'accumulate' : is not a member of 'std'
'accumulate': identifier not found
Why is 'accumulate' not a member of 'std' ?
ps:我知道我可以使用“sum += array[i]”方式而不是使用累积,但我想了解这里发生了什么以及如何使我的示例工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试添加
它将引入您正在寻找的“std::accumulate”函数。
更进一步,您将遇到找出数组中元素数量的问题。事实上,不能将数组传递给函数并希望函数能够知道数组的大小。它将衰减为指针。因此,您的
count
计算将是错误的。如果您希望能够传递实际大小的指定数组,则必须使用模板化函数。Try to add
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.这不完全是您问的问题,但您的代码示例中存在一个容易出现的错误。 Accumulate 中的初始值是模板化的,并且在您的代码中它被模板化为整数。如果你向它传递一组双精度数,它们将被转换为整数,并且你将得到错误的答案。以前犯过这个错误,我给自己做了如下快速保证:
我想如果有人感兴趣的话我会分享它。
为了完整起见,您的函数可以如下所示:
或者要格外小心
,或者更好的是 Didier Trosset 的模板版本
Its not quite the question you asked, but there is an easy bug to make in your code sample. The initial value in
accumulate
is templated, and in your code its templated to integers. If you pass it a set of doubles, these will be cast to integers and you will get the wrong answers. Having made this mistake before, I made myself a quick guarentee as follows:Thought I would share it in case it is of interest.
Just for completeness, your function can look like:
or to be extra careful
or better still the templated version from Didier Trosset
要使用 std::accumulate ,您需要包含适当的标头。将以下内容添加到您的源文件中。
To use
std::accumulate
you need to include the appropriate header. Add the following to your source file.或者
如果将数组传递给函数,则会“丢失”其大小,因此必须将其作为参数传递(它比这更复杂一点......但现在应该足够了)
or
If you pass an array to a function, you "lose" its size, so you have to pass it as a parameter (it's a little more complex than this... but for now it should be enough)