简体   繁体   English

检索以C ++返回数组的函数的输出

[英]Retrieving the output of a function that returns an array in C++

How can I retrieve the output of the following function so that I can use it. 如何检索以下函数的输出,以便可以使用它。

my code: 我的代码:

#include <iostream>
    #include <iomanip>
    #include <complex>
    #include <cmath>
    #include <cstddef>

    double binFreq(int n)
         {
            int j;
            double* f = new double[n];

            for ( j = 0 ; j < n ; j++ ){

            f[j] =(fmod(j+(floor(n/2)), n)-floor(n/2))/n;
            //std::cout << "f["<<j<<"] ="<<f[j] <<std::endl;
            }
            delete [] f;
         }

    int main()
    {   

        int n=9;
        double* F=new double [n];
        F[n]=binFreq(n);

        for ( int i = 0 ; i < n ; ++i ){ 
        std::cout << "F["<<i<<"] ="<<F[i] <<std::endl;
        }


    }

As you can see in the code above , I have tried but I am getting all zeros for each element: 如您在上面的代码中所看到的,我已经尝试过,但是每个元素的全零是:

Output:

F[0] =0
F[1] =0
F[2] =0
F[3] =0
F[4] =0
F[5] =0
F[6] =0
F[7] =0
F[8] =0

Modified code: 修改后的代码:

#include <iostream>
#include <cmath>
#include <cstddef>
#include <vector>

std::vector<double> binFreq(int n)
{
   int j;
   std::vector<double> f(n);

   for ( j = 0 ; j < n ; j++ ){

      f[j] =(fmod(j+(floor(n/2)), n)-floor(n/2))/n;
   }
   return f;
}

int main()
{   

    int n=9;
    double* F;
    F=binFreq(n);

    for ( int i = 0 ; i < n ; ++i ){ 
    std::cout << "F["<<i<<"] ="<<F[i] <<std::endl;
    }

}

I am getting new error main.cpp: In function 'int main()': main.cpp:23:16: error: cannot convert 'std::vector' to 'double*' in assignment F=binFreq(n); 我收到新的错误main.cpp:在函数'int main()'中:main.cpp:23:16:错误:无法在赋值F = binFreq(n)中将'std :: vector'转换为'double *';

It's better to avoid returning an array. 最好避免返回数组。 Return a std::vector instead. 返回一个std::vector It is less error prone than using arrays. 它比使用数组更容易出错。 Also, dynamic memory management is taken care of for you. 此外,动态内存管理将为您服务。

std::vector<double> binFreq(int n)
{
   int j;
   std::vector<double> f(n);

   for ( j = 0 ; j < n ; j++ ){

      f[j] =(fmod(j+(floor(n/2)), n)-floor(n/2))/n;
   }
   return f;
}

You'll need to modify main to reflect the return value of the function. 您将需要修改main以反映该函数的返回值。

int main()
{   
   int n = 9;
   auto F = binFreq(n);
   for ( int i = 0 ; i < n ; ++i )
   { 
      std::cout << "F["<<i<<"] ="<<F[i] <<std::endl;
   }
}

You should create the array F inside your function (as you do), but then not delete it, rather return it. 您应该在函数内部创建数组F(如您所做的那样),但不要删除它,而要返回它。 return f; instead of delete [] f; 而不是delete [] f;

Then in your main function, just declare F as double* F and use the assignement F = binFreq(n); 然后在主函数中,将F声明为double* F并使用F = binFreq(n);

That way, you create the array inside of your function, and you return a pointer to it. 这样,您可以在函数内部创建数组,然后返回指向它的指针。 Then from your main, you assign that pointer to F and you can then use your array. 然后从您的主体中,将该指针分配给F,然后可以使用数组。

Don't forget to delete the array at the end of your main! 不要忘记在主目录的最后删除数组!

You can also give a reference to a vector: 您还可以参考向量:

void binFreq(std::vector<double> &freq, int n)
{
    freq.resize(n,0.0) ;
    for (int j = 0 ; j < n ; j++ )
    {
        f[j] =(fmod(j+(floor(n/2)), n)-floor(n/2))/n;
    }
}

int main()
{
    int n=9 ;
    std::vector<double> F ;
    binFreq(F,n) ;

    return 0 ;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM