I have a function that takes in vectors as parameters and then converts them into array and then calls a third party library function. I searched that to copy the contents of the vector into an array, this is one way:
double* data = new double[svr.voice.size()];//voice is the vector which is a global variable in a class SVR
copy(svr.voice.begin(), svr.voice.end(), data);
However, only the first element is being copied. I repeatedly call this function inside a while loop and every time the vector size increases by 1, but the array just always contains the first index.
Any ideas why? Any help would be greatly appreciated! Thanks!
If you don't want to make a copy of the elements you don't have to. You can take the address of the first element in the vector and pass this to the function expecting an array:
Something a bit like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void process_array(double* array, size_t size)
{
for(size_t i = 0; i < size; ++i)
{
// ... do stuff to array[i];
}
}
int main()
{
std::vector<double> v;
v.push_back(3.14);
v.push_back(6.2);
v.push_back(0.9);
process_array(&v[0], v.size());
}
Thanks for your replies. Apparently, my code already works. I just thought that my array has only one value that's why the library function is not working. Sorry for the trouble.