从float *转换为vector< float> [英] convert from float* to vector<float >
问题描述
我有一个功能
float * pointwise_search(vector<float > &P,vector<float > &Q,float* n, int len )
.
我想用matlab调用它,所以我需要编写一个mexFunction.
I want matlab call it so I need to write a 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 );
}
但是我发现vector<float > Numbers= (vector<float >)mxGetPr(prhs[0]);
vector<float > Q= (vector<float >)mxGetPr(prhs[1]);
是错误的.
But I found that vector<float > Numbers= (vector<float >)mxGetPr(prhs[0]);
vector<float > Q= (vector<float >)mxGetPr(prhs[1]);
are wrong.
所以我必须将float * pointwise_search(vector<float > &P,vector<float > &Q,float* n, int len )
更改为float * pointwise_search(float *P,float *Q,float* n, int len )
.
根据答案,我将其重写为以下内容
According to the answers, I rewrite as the following
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();
...
}
但是会发生错误.
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
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);
. :)
As the comment,I should change vector<float> P{p, p + num_thres};
to vector<float> P(p, p + num_thres);
. :)
推荐答案
当然,您通常不能将指针转换为vector
,它们是不同的东西.但是,如果指针包含已知长度的C样式数组的第一个元素的地址,则可以创建一个vector
,其内容与该数组相同,如下所示:
Of course you can not generally convert a pointer to a vector
, they are different things. If however the pointer holds the address of the first element of a C-style array of known length, you can create a vector
with the same contents as the array like:
std::vector<float> my_vector {arr, arr + arr_length};
其中,arr
是指针,而arr_length
是数组的长度.然后,您可以将vector
传递给期望std::vector<float>&
的函数.
where arr
is said pointer and arr_length
is the length of the array. You can then pass the vector
to the function expecting std::vector<float>&
.
这篇关于从float *转换为vector< float>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!