Connecting Tech Pros Worldwide Forums | Help | Site Map

Initialization of array by vector

Alex Vinokur
Guest
 
Posts: n/a
#1: Jul 22 '05
Is there any way to initialize array by vector ?

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

--
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html





Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Initialization of array by vector


Alex Vinokur wrote:
[color=blue]
> Is there any way to initialize array by vector ?
>
> vector<double> v (10, 1.23);
> double a[] = <initialization by 'v'>[/color]

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.

Chris \( Val \)
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Initialization of array by vector



"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:btbn30$vgi$05$1@news.t-online.com...
| 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


Rolf Magnus
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Initialization of array by vector


Chris ( Val ) wrote:
[color=blue]
>
> "Rolf Magnus" <ramagnus@t-online.de> wrote in message
> news:btbn30$vgi$05$1@news.t-online.com...
> | 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.[/color]

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

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

Chris \( Val \)
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Initialization of array by vector



"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:btbnot$vgi$05$2@news.t-online.com...
| Chris ( Val ) wrote:
|
| >
| > "Rolf Magnus" <ramagnus@t-online.de> wrote in message
| > news:btbn30$vgi$05$1@news.t-online.com...
| > | 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


Robert Paul Clark
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Initialization of array by vector


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

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

Closed Thread