Reputation: 17
I am getting an exception with the following code snippet:
// matlab mex program ...
float *ptr=new float[n*m];
.
.
.
std::vector<float> v(n*m);
v.assign(ptr);
How do I correctly assign ptr
to the vector v
?
Upvotes: 0
Views: 55
Reputation: 15997
You can use either the constructor or assign, as you tried. However, you need to pass the length in some way. In the standard library, it's a common idiom to do this by passing the begin and end of a sequence - but those can be bare pointers. Like this:
std::vector<float> v; // Note you do not need the size here
v.assign(ptr, ptr+n*m);
or just:
std::vector<float> v(ptr, ptr+n*m);
It also seems like you have an error in your allocation:
float *ptr=new float[n*m]; // Allocate floats, not pointers to floats
Upvotes: 2