我有一个函数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 );
}但我发现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();
...
} 但也会出现错误。
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);。:)
发布于 2014-07-24 03:02:08
当然,通常不能将指针转换为vector,它们是不同的东西。但是,如果指针包含已知长度的C样式数组的第一个元素的地址,则可以创建与该数组具有相同内容的vector,如下所示:
std::vector<float> my_vector {arr, arr + arr_length};其中arr是所说的指针,arr_length是数组的长度。然后,您可以将vector传递给期望使用std::vector<float>&的函数。
发布于 2014-07-24 03:03:38
如果你看一下例如this std::vector constructor reference,你会看到一个构造函数,它接受两个迭代器(链接参考中的替代4)。此构造函数可用于从另一个容器构造向量,包括数组。
例如:
float* pf = new float[SOME_SIZE];
// Initialize the newly allocated memory
std::vector<float> vf{pf, pf + SOME_SIZE};https://stackoverflow.com/questions/24918686
复制相似问题