使用accumulate计算数组double[]平均值的函数

发布于 2024-12-11 19:46:25 字数 920 浏览 0 评论 0原文

它一定是最常见的函数,每个人在某处都有代码片段,但我实际上花了不少于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2024-12-18 19:46:25

尝试添加

#include <numeric>

它将引入您正在寻找的“std::accumulate”函数。

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

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

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);
}
星星的軌跡 2024-12-18 19:46:25

这不完全是您问的问题,但您的代码示例中存在一个容易出现的错误。 Accumulate 中的初始值是模板化的,并且在您的代码中它被模板化为整数。如果你向它传递一组双精度数,它们将被转换为整数,并且你将得到错误的答案。以前犯过这个错误,我给自己做了如下快速保证:

  /** Check that not inputting integer type into accumulate
   *  This is considered an error in this program (where a double was expected
   *  @tparam InputIterator The iterator to accumulate
   *  @tparam T The type to accumulate - will fail if integer.
   *  @param first The first iterator to accumulate from.
   *  @param last the iterator to acculate to,
   *  @param init The initial value
   *  @return The accumulated value as evaluated by std::accumulate.
   */
  template<class InputIterator, class T>
  inline
  T
  accumulate_checked(InputIterator first, InputIterator last, T init )
  {
    return std::accumulate(first,last, init);
  }

  //Not implemented for integers (will not compile if called).
  template<class InputIterator>
  inline
  int
  accumulate_checked(InputIterator first, InputIterator last, int init );

我想如果有人感兴趣的话我会分享它。

为了完整起见,您的函数可以如下所示:

double mean_array( double *array, size_t count )
{
    double sum = std::accumulate(array,array+count,0.0)
    return sum / count;
}

或者要格外小心

double mean_array( double *array, size_t count )
{
    double sum = accumulate_checked(array,array+count,0.0)
    return sum / count;
}

,或者更好的是 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:

  /** Check that not inputting integer type into accumulate
   *  This is considered an error in this program (where a double was expected
   *  @tparam InputIterator The iterator to accumulate
   *  @tparam T The type to accumulate - will fail if integer.
   *  @param first The first iterator to accumulate from.
   *  @param last the iterator to acculate to,
   *  @param init The initial value
   *  @return The accumulated value as evaluated by std::accumulate.
   */
  template<class InputIterator, class T>
  inline
  T
  accumulate_checked(InputIterator first, InputIterator last, T init )
  {
    return std::accumulate(first,last, init);
  }

  //Not implemented for integers (will not compile if called).
  template<class InputIterator>
  inline
  int
  accumulate_checked(InputIterator first, InputIterator last, int init );

Thought I would share it in case it is of interest.

Just for completeness, your function can look like:

double mean_array( double *array, size_t count )
{
    double sum = std::accumulate(array,array+count,0.0)
    return sum / count;
}

or to be extra careful

double mean_array( double *array, size_t count )
{
    double sum = accumulate_checked(array,array+count,0.0)
    return sum / count;
}

or better still the templated version from Didier Trosset

最初的梦 2024-12-18 19:46:25

要使用 std::accumulate ,您需要包含适当的标头。将以下内容添加到您的源文件中。

#include <numeric>

To use std::accumulate you need to include the appropriate header. Add the following to your source file.

#include <numeric>
舟遥客 2024-12-18 19:46:25
double mean_array( double *array, size_t count )
{
    double sum = 0.0;

    for (size_t i = 0; i < count; i++)
    {
        sum += array[i];
    }

    return sum / count;
}

或者

double mean_array( double *array, size_t count )
{
    double sum = 0.0;
    double *pastLast = array + count;

    while (array < pastLast)
    {
        sum += *array;
        array++;
    }

    return sum / count;
}

如果将数组传递给函数,则会“丢失”其大小,因此必须将其作为参数传递(它比这更复杂一点......但现在应该足够了)

double mean_array( double *array, size_t count )
{
    double sum = 0.0;

    for (size_t i = 0; i < count; i++)
    {
        sum += array[i];
    }

    return sum / count;
}

or

double mean_array( double *array, size_t count )
{
    double sum = 0.0;
    double *pastLast = array + count;

    while (array < pastLast)
    {
        sum += *array;
        array++;
    }

    return sum / count;
}

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文