Initialization of array by vector

A

Alex Vinokur

Is there any way to initialize array by vector ?

vector<double> v (10, 1.23);
double a[] = <initialization by 'v'>
 
R

Rolf Magnus

Alex said:
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);

//later
delete [] a;


All known implementations of vector store their elements just as array
internally, and that was actually the intended behaviour in the
standard. So you can just take a pointer to the first element of your
vector and use it as pointer to the first element of an array of
v.size() elements.
 
R

Rolf Magnus

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.

Right. I forgot to mention that, but for doubles, it doesn't make a
difference.
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 :).

You could always write a program that auto-generates that code :)
 
C

Chris \( Val \)

| 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 :).

Cheers.
Chris Val
 
C

Chris \( Val \)

| Chris ( Val ) wrote:
|
| >
| > | > | 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.
|
| Right. I forgot to mention that, but for doubles, it doesn't make a
| difference.
|
| > 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 :).
|
| You could always write a program that auto-generates that code :)

Yeah :).

std::generate_n() or std::fill() come to mind <G>.

Cheers.
Chris Val
 
R

Robert Paul Clark

Is there any way to initialize array by vector ?
vector<double> v (10, 1.23);
double a[] = <initialization by 'v'>

double x[] = { 1.0, 3.0, 5.0, 7.0, 9.0, 99.0 };
vector<double> y(x, x+5); // or y(x, x+sizeof(x)/sizeof(*x))

Bob Clark
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top