Using valarray

B

Busin

valarray<bool> va1(10);

// Initialize va1 here?

valarray<bool> va2 (80);

va2 = va1;

Do we have to initialize va1 like using for loop above (since if not, the
printout of va1 will display garbage)?

Can va1 be initialized like "valarray<bool> va1(10, false);" when defining
it?

Is it ok to assign va1 to va2? Does va2 have to be specified with a size
before assigning va1 to it? Must the size of va2 be larger than va1?

Thanks!
 
J

John Harrison

Busin said:
valarray<bool> va1(10);

// Initialize va1 here?

valarray<bool> va2 (80);

va2 = va1;

Do we have to initialize va1 like using for loop above (since if not, the
printout of va1 will display garbage)?

No, the elements of va1 will be default constructed. I believe for a bool
this should mean it will be set to false. Your compiler may not implement
this correctly though.
Can va1 be initialized like "valarray<bool> va1(10, false);" when defining
it?

No, like this

valarray said:
Is it ok to assign va1 to va2?
Yes

Does va2 have to be specified with a size
before assigning va1 to it?
No

Must the size of va2 be larger than va1?

No, va2 will change its size to be the same as va1

john
 
J

John Harrison

tom_usenet said:
No, it's undefined behaviour, since the lengths don't match.


The sizes of va2 and va1 must be exactly the same. See 26.3.2.2.

OK, must have been getting confused with vector. Apologies.

john
 

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,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top