不能从函数返回向量
C++ - Cannot return vector from function
假设以下函数
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;
}
我在下面的代码中调用这个函数
void Analyze(){
int points = 90000000;//hSignal->GetNbinsX();
double* x = new double[points]; // SIZE limited only by OS/Hardware
double* y = new double[points];
std::vector<double> left;
std::vector<double> right;
double threshold = 6234.34;
Function_to_Fill_X_and_Y();
LocatePulseEdges(points, x, y, threshold, left, right);
cout << "First left threshold crossing @ time : " << left[0] << endl;
}
虽然我没有得到编译错误,但当我运行程序时,它在返回语句之前崩溃了。
知道为什么会这样吗?
LocatePulseEdges
函数和Analyze
函数存在几个缺陷。
首先,如果你打算在一段代码中使用std::vector<double>
,为什么不在整个代码中使用它呢?你有:
void Analyze()
{
//...
double* x = new double[points];
double* y = new double[points];
//...
}
除非您调用了delete [] x
和delete [] y
,否则该函数存在内存泄漏。你可以直接使用
std::vector<double> x(points), y(points);
和在填充它们的函数中,如果使用c++ 11,则传递x.data()
和y.data()
,如果使用c++ 11,则传递&x[0]
和&y[0]
。这减轻了内存泄漏。
即使你在某个地方有delete []
,如果抛出异常,delete []
可能会被绕过,导致泄漏。使用std::vector
,即使出现异常,vector
也会被销毁。
第二,对于
LocatePulseEdges
函数,传递vector的by (const)引用,而不是value引用。此外,不需要按值返回向量。如果你在函数中创建了一个全新的向量,那么可能会证明返回新向量是合理的,但是你没有这样做。所以返回一个void
。
void LocatePulseEdges(int points, double* signal_x, double* signal_y, double threshold, vector<double>& left_edge, vector<double>& right_edge)
{
//...
}
当按值传递vector时,会生成vector的副本,因此left_edge.push_back()
调用处理的是临时vector,而不是传递的实际vector。这就是为什么在返回时,向量left_edge
是空的。
最后,如果要访问vector中的第一项,检查vector::empty()
。不能仅仅假定该元素存在于vector中。
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;
上面的代码假设您接受了对x
和y
变量使用std::vector<double>
的建议。
通过引用传递你的left_edge,这样它就可以被你的函数修改:
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
}
当你给出没有&
的参数时,程序复制你的参数的值,然后使用它。所以你不能修改它。使用&
传递参数称为通过引用传递。
当你通过引用传递一个对象时,你可以修改它,这样会更快。如果你需要传递一个对象而不修改它,给一个const ref:
void foo(const std::vector<double> &vec)
比
快void foo(std::vector<double> vec)
,并通过const
关键字阻止您修改vec
。
附加说明:
void
函数不需要任何返回值,但在许多情况下,最好使用Bool函数
Bool LocatePulseEdges()
{
//do your stuff
return True ;
}
这样做可以允许您在出现错误时过早返回False。
根据评论我得到:
-
将LocatePulseEdges函数更改为使用引用:
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 }
所以你可以在函数 中改变参数的值 在访问vector之前检查元素是否存在:
if (left.size() <= index) { return left[index]; }
你的代码出了什么问题?
for (int i=0; i<points; i++)
被执行,因为points = 9000000
,但signal_y[i]<threshold
永远不会为真(它没有在你的代码中初始化),所以没有插入到left
。
- 矩阵向量乘法(cublasDgemv)返回零
- C++中函数的向量返回类型引发错误
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 从 std<Derived>::shared_ptr 的向量返回 std::shared_ptr<Base>
- 从对象的向量返回某个类的对象
- C++ 将向量中出现 n 次的所有元素作为向量返回
- 向量返回负大小 c++
- 如何从向量返回给定类型的元素?
- 将对象的向量返回到函数
- 尝试从向量返回对象时出现"没有可行的重载运算符 [] 错误
- 调用函数时,如何通过向量返回类型
- 向量返回/加法算法的问题
- 在向量返回上移动语义行为
- 从int c++的向量返回一个无符号长
- 向量返回空 C++
- 从融合向量的std向量返回列,无需复制
- C++为空向量上的向量返回什么::back()
- 函数的向量返回
- 如何用适当的OO有效地将100万的私有向量返回给其他几个类