C++ C++;-无法从函数返回向量

C++ C++;-无法从函数返回向量,c++,function,vector,return,C++,Function,Vector,Return,承担以下功能 std::vector<double> LocatePulseEdges(int points, double* signal_x, double* signal_y, double threshold, vector<double> left_edge, vector<double> right_edge){ cout << "1" << endl; for (int i=0; i<points; i++)

承担以下功能

std::vector<double> LocatePulseEdges(int points, double* signal_x, double* signal_y, double threshold, vector<double> left_edge, vector<double> right_edge){
cout << "1" << endl;
    for (int i=0; i<points; i++){
        if(signal_y[i]<threshold){// left side of the pulse
            left_edge.push_back(signal_x[i]);
            break;
        }
        if(signal_y[i]>threshold){// right side of the pulse
            right_edge.push_back(signal_x[i]);
            break;
        }
    }
cout << "6" << endl;
    return left_edge;
    //return right_edge;

cout << "7" << endl;
}
std::向量定位脉冲(int点、双*信号x、双*信号y、双阈值、向量左边缘、向量右边缘){

不能通过引用传递您的左边缘,以便您的操作可以修改它:

void LocatePulseEdges((int points, double* signal_x, double* signal_y, double threshold, 
std::vector<double> &left_edge, std::vector<double> &right_edge)
{
    //do your stuff
}
快于

void foo(std::vector<double> vec)

这样做可以让您在出现任何问题时提前返回False。

根据评论,我得到:

  • 将LocatePulseEdges函数更改为使用以下引用:

    void locatepulsedges((整数点,双*信号x,双*信号y,双阈值,
    标准::向量和左边缘,标准::向量和右边缘)
    {
    //做你的事
    }
    
    所以您可以在函数内部更改参数的值

  • 在访问向量之前,请检查向量中是否存在元素:


    if(left.size()在
    locatepulsedges
    函数以及
    Analyze
    函数中存在多个缺陷

    首先,如果您要在代码的一个部分中使用
    std::vector
    ,为什么不通篇使用它呢?您有:

    void Analyze()
    {
      //...        
      double* x = new double[points]; 
      double* y = new double[points];
     //...
    }
    
    除非调用了
    delete[]x
    delete[]y
    ,否则此函数存在内存泄漏。您可以简单地使用

    std::向量x(点),y(点);

    在填充它们的函数中,如果使用C++11,则传递
    x.data()
    y.data()
    ,如果不使用,则传递
    &x[0]
    &y[0]
    。这样可以减少内存泄漏

    即使您确实在某个地方有
    delete[]
    ,如果引发异常,该
    delete[]
    也可能被绕过,从而导致泄漏。使用
    std::vector
    ,即使发生异常,
    vector
    也会被销毁


    其次,对于
    locatepulsedges
    函数,将向量的值传递给(const)引用,而不是按值。此外,不需要按值返回向量。如果您在函数中创建一个全新的向量,那么这可能会证明返回新向量是正确的,但您没有这样做。因此,返回一个
    void

    void LocatePulseEdges(int points, double* signal_x, double* signal_y, double threshold, vector<double>& left_edge, vector<double>& right_edge)
    {
        //...
    }
    

    上面的代码假设您接受了对
    x
    y
    变量使用
    std::vector
    的建议。

    @Boiethios:Hmmm…我该如何检查?如果是这样,我该如何解决?对不起,我猜不是这样。@Thanos为什么按值传递向量?@GMichael:我还能做什么?我不太熟悉h指针…:(@Thanos阅读关于参考。。。
    Bool LocatePulseEdges()
    {
        //do your stuff
        return True ;
    }
    
    void Analyze()
    {
      //...        
      double* x = new double[points]; 
      double* y = new double[points];
     //...
    }
    
    void LocatePulseEdges(int points, double* signal_x, double* signal_y, double threshold, vector<double>& left_edge, vector<double>& right_edge)
    {
        //...
    }
    
       LocatePulseEdges(points, x.data(), y.data(), threshold, left, right);
       if ( !left.empty() ) 
           cout << "First left threshold crossing @ time : " << left[0] << endl;
       else
           cout << "No threshold's generated" << endl;