A
Alex Vinokur
Is there any way to initialize array by vector ?
vector<double> v (10, 1.23);
double a[] = <initialization by 'v'>
vector<double> v (10, 1.23);
double a[] = <initialization by 'v'>
Alex said:Is there any way to initialize array by vector ?
vector<double> v (10, 1.23);
double a[] = <initialization by 'v'>
Chris said:| Alex Vinokur wrote:
|
| > Is there any way to initialize array by vector ?
| >
| > vector<double> v (10, 1.23);
| > double a[] = <initialization by 'v'>
|
| No. You didn't specify a size for a, so it must get the size from
| the initializer, which must be known at compile time. The closest
| you can get would be a dynamically allocated array. Something like:
|
| std::vector<double> v (10, 1.23);
| double* a = new double[v.size()];
| std::copy(v.begin(), v.end(), a);
You could do that, but it is not initialisation.
You can always initialise as follows:
std::vector<double> v ( 10, 1.23 );
double a[] = { v[0], v[1], v[2], v[3]...... };
...but this is inappropriate for large arrays .
vector<double> v (10, 1.23);
double a[] = <initialization by 'v'>
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.