473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Initialization of array by vector

Is there any way to initialize array by vector ?

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

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


Jul 22 '05 #1
5 1746
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);

//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.

Jul 22 '05 #2

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bt*************@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
Jul 22 '05 #3
Chris ( Val ) wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bt*************@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 :-)

Jul 22 '05 #4

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bt*************@news.t-online.com...
| Chris ( Val ) wrote:
|
| >
| > "Rolf Magnus" <ra******@t-online.de> wrote in message
| > news:bt*************@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
Jul 22 '05 #5
> 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

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Russell Silva | last post by:
I have a class A with a member variable type vector<foo> which is initialized upon construction (no standard () constructor): class A { protected: vector<foo> foo_vector; public: A(const...
5
by: cppaddict | last post by:
Is it possible to avoid using push_back repeatedly when initializing a vector? That is, is there a vector syntax that would be analogous to the following array initialization syntax: MyClass...
4
by: junger | last post by:
If a member object is static, is it possible to initialize it with function calls? E.g., if I want my class to contain a static vector filled with other static objects, can I fill this vector...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
8
by: iluvatar | last post by:
Hi all. How can I initialize an array data member in the "faster" way? For example, suppose I have a class like class Example{ private: double array; public: Example(const double & val0,...
5
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
15
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
12
by: pauldepstein | last post by:
Experimenting at home with visual c++, I see that int main() {std::vector<doublevect(5);} creates a vector whose 5 initial values are all 0. Is this standard or might the five initial values be...
2
by: aaragon | last post by:
Hello everyone, Is this valid? template <class A> struct ClassA { typedef A value_type; value_type* data_;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.