Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert vector to array

Tags:

c++

arrays

vector

How do I convert a std::vector<double> to a double array[]?

like image 207
ganuke Avatar asked May 27 '10 17:05

ganuke


People also ask

How do I turn a vector into an array?

Using the transform() function to convert vector to array in C++ We can use the transform() function in C++ to apply a function to all the elements of an array. It applies the function to all the elements and stores the result in a new array.

Can vectors be used as arrays?

It is a row of data. An array is a list that is arranged in multiple dimensions. A two-dimensional array is a vector of vectors that are all of the same length. We see that A has three rows and four columns and that each row is one of the vectors in the list.


1 Answers

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]; 
like image 166
Michael Mrozek Avatar answered Sep 23 '22 06:09

Michael Mrozek