Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using valarray

Reply
Thread Tools

Using valarray

 
 
Busin
Guest
Posts: n/a
 
      06-29-2004
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!



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-29-2004

"Busin" <> wrote in message
news:8T8Ec.158035$...
> 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<bool> val(false, 10);

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


 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      06-29-2004
On Tue, 29 Jun 2004 09:19:49 +0100, "John Harrison"
<> wrote:

>
>"Busin" <> wrote in message
>news:8T8Ec.158035$...
>> valarray<bool> va1(10);
>>
>> // Initialize va1 here?
>>
>> valarray<bool> va2 (80);
>>
>> va2 = va1;
>>
>> Is it ok to assign va1 to va2?

>
>Yes


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

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

>
>No


Yes.

>> Must the size of va2 be larger than va1?

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


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

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-29-2004

"tom_usenet" <> wrote in message
news:...
> On Tue, 29 Jun 2004 09:19:49 +0100, "John Harrison"
> <> wrote:
>
> >
> >"Busin" <> wrote in message
> >news:8T8Ec.158035$...
> >> valarray<bool> va1(10);
> >>
> >> // Initialize va1 here?
> >>
> >> valarray<bool> va2 (80);
> >>
> >> va2 = va1;
> >>
> >> Is it ok to assign va1 to va2?

> >
> >Yes

>
> No, it's undefined behaviour, since the lengths don't match.
>
> >> Does va2 have to be specified with a size
> >> before assigning va1 to it?

> >
> >No

>
> Yes.
>
> >> Must the size of va2 be larger than va1?

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

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Slicing std::valarray using references? MrMF C++ 0 09-01-2009 04:31 PM
Why not use valarray<valarray<T> > ? Jim West C++ 2 12-23-2003 03:54 PM
Enhancing valarray with "normal" arithmetic operators =?ISO-8859-1?Q?Christian_Brechb=FChler?= C++ 6 09-14-2003 06:39 PM
Re: valarray resize Marc Schellens C++ 5 07-30-2003 07:06 AM
New Altivec-optimized valarray implementation Glen Low C++ 3 06-26-2003 05:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57