Initialization of array by vector | | | 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. | | | | 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 | | | | 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 :-) | | | | 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 | | | | 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|