我有一个功能
float * pointwise_search(vector<float > &P,vector<float > &Q,float* n, int len )
.
我想matlab调用它所以我需要编写一个mexFunction.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs != 4)
{
mexErrMsgTxt("Input is wrong!");
}
float *n = (float*) mxGetData(prhs[2]);
int len = (int) mxGetScalar(prhs[3]);
vector<float > Numbers= (vector<float >)mxGetPr(prhs[0]);
vector<float > Q= (vector<float >)mxGetPr(prhs[1]);
plhs[1] = pointwise_search(Numbers,Q,n,len );
}
Run Code Online (Sandbox Code Playgroud)
但我发现这vector<float > Numbers= (vector<float >)mxGetPr(prhs[0]);
vector<float > Q= (vector<float >)mxGetPr(prhs[1]);
是错误的.
所以我必须float * pointwise_search(vector<float > &P,vector<float > &Q,float* n, int len )
改为float * pointwise_search(float *P,float *Q,float* n, int len )
.
根据答案,我改写如下
float * pointwise_search(float p,float *q,int num_thres, float n, int len )
{ vector<float> P{p, p + num_thres};
vector<float> Q{q, q + num_thres};
int size_of_threshold = P.size();
...
}
Run Code Online (Sandbox Code Playgroud)
但是会出现错误.
pointwise_search.cpp(12) : error C2601: 'P' : local function definitions are illegal
pointwise_search.cpp(11): this line contains a '{' which has not yet been matched
作为评论,我应该vector<float> P{p, p + num_thres};
改为vector<float> P(p, p + num_thres);
.:)
当然你通常不能将指针转换为a vector
,它们是不同的东西.但是,如果指针保存已知长度的C样式数组的第一个元素的地址,则可以使用vector
与数组相同的内容创建a :
std::vector<float> my_vector {arr, arr + arr_length};
Run Code Online (Sandbox Code Playgroud)
arr
指针在哪里,是arr_length
数组的长度.然后,您可以将vector
期望传递给函数std::vector<float>&
.
归档时间: |
|
查看次数: |
5600 次 |
最近记录: |