Using valarray

Discussion in 'C++' started by Busin, Jun 29, 2004.

  1. Busin

    Busin Guest

    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!
    Busin, Jun 29, 2004
    #1
    1. Advertising

  2. "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
    John Harrison, Jun 29, 2004
    #2
    1. Advertising

  3. Busin

    tom_usenet Guest

    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
    tom_usenet, Jun 29, 2004
    #3
  4. "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
    John Harrison, Jun 29, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glen Low
    Replies:
    3
    Views:
    980
    Glen Low
    Jun 26, 2003
  2. Marc Schellens

    Re: valarray resize

    Marc Schellens, Jul 29, 2003, in forum: C++
    Replies:
    5
    Views:
    1,282
    Marc Schellens
    Jul 30, 2003
  3. =?ISO-8859-1?Q?Christian_Brechb=FChler?=

    Enhancing valarray with "normal" arithmetic operators

    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Sep 12, 2003, in forum: C++
    Replies:
    6
    Views:
    957
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Sep 14, 2003
  4. Jim West
    Replies:
    2
    Views:
    498
    Jim West
    Dec 23, 2003
  5. MrMF

    Slicing std::valarray using references?

    MrMF, Sep 1, 2009, in forum: C Programming
    Replies:
    0
    Views:
    349
Loading...

Share This Page