I want to convert vector of double to array of double. Can any one help me to do this
vector<double> thevector;
//...
double *thearray = &thevector[0];
This is guaranteed to work by the standard, however there are some caveats: in particular take care to only use thearray while thevector is in scope.
There's a fairly simple trick to do so, since the spec now guarantees vectors store their elements contiguously:
std::vector<double> v;
double* a = &v[0];
Vectors effectively are arrays under the skin. If you have a function:
void f( double a[]);
you can call it like this:
vector <double> v;
v.push_back( 1.23 )
f( &v[0] );
You should not ever need to convert a vector into an actual array instance.
What for? You need to clarify: Do you need a pointer to the first element of an array, or an array?
If you're calling an API function that expects the former, you can do do_something(&v[0], v.size()), where v is a vector of doubles. The elements of a vector are contiguous.
Otherwise, you just have to copy each element:
double arr[100];
std::copy(arr, arr + 100, v.begin());
Of course, ensure v actually has enough elements to copy.
Utility function, to help with arrays:
template <typename T, size_t N>
T* end(T (&pArray)[N])
{
return &pArray[0] + N;
}
Giving:
double arr[100];
std::copy(arr, end(arr), v.begin());