Using valarray

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Busin

    Using valarray

    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<b ool> 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!



  • John Harrison

    #2
    Re: Using valarray


    "Busin" <businm@fidrep. com> wrote in message
    news:8T8Ec.1580 35$Gx4.68371@bg tnsc04-news.ops.worldn et.att.net...[color=blue]
    > 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)?[/color]

    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.
    [color=blue]
    >
    > Can va1 be initialized like "valarray<b ool> va1(10, false);" when defining
    > it?[/color]

    No, like this

    valarray<bool> val(false, 10);
    [color=blue]
    >
    > Is it ok to assign va1 to va2?[/color]

    Yes
    [color=blue]
    > Does va2 have to be specified with a size
    > before assigning va1 to it?[/color]

    No
    [color=blue]
    > Must the size of va2 be larger than va1?[/color]

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

    john


    Comment

    • tom_usenet

      #3
      Re: Using valarray

      On Tue, 29 Jun 2004 09:19:49 +0100, "John Harrison"
      <john_andronicu s@hotmail.com> wrote:
      [color=blue]
      >
      >"Busin" <businm@fidrep. com> wrote in message
      >news:8T8Ec.158 035$Gx4.68371@b gtnsc04-news.ops.worldn et.att.net...[color=green]
      >> valarray<bool> va1(10);
      >>
      >> // Initialize va1 here?
      >>
      >> valarray<bool> va2 (80);
      >>
      >> va2 = va1;
      >>
      >> Is it ok to assign va1 to va2?[/color]
      >
      >Yes[/color]

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

      Yes.
      [color=blue][color=green]
      >> Must the size of va2 be larger than va1?[/color]
      >
      >No, va2 will change its size to be the same as va1[/color]

      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

      Comment

      • John Harrison

        #4
        Re: Using valarray


        "tom_usenet " <tom_usenet@hot mail.com> wrote in message
        news:7fb2e0ti5g 4ps5d32jtl5rr79 s4h6pi01k@4ax.c om...[color=blue]
        > On Tue, 29 Jun 2004 09:19:49 +0100, "John Harrison"
        > <john_andronicu s@hotmail.com> wrote:
        >[color=green]
        > >
        > >"Busin" <businm@fidrep. com> wrote in message
        > >news:8T8Ec.158 035$Gx4.68371@b gtnsc04-news.ops.worldn et.att.net...[color=darkred]
        > >> valarray<bool> va1(10);
        > >>
        > >> // Initialize va1 here?
        > >>
        > >> valarray<bool> va2 (80);
        > >>
        > >> va2 = va1;
        > >>
        > >> Is it ok to assign va1 to va2?[/color]
        > >
        > >Yes[/color]
        >
        > No, it's undefined behaviour, since the lengths don't match.
        >[color=green][color=darkred]
        > >> Does va2 have to be specified with a size
        > >> before assigning va1 to it?[/color]
        > >
        > >No[/color]
        >
        > Yes.
        >[color=green][color=darkred]
        > >> Must the size of va2 be larger than va1?[/color]
        > >
        > >No, va2 will change its size to be the same as va1[/color]
        >
        > The sizes of va2 and va1 must be exactly the same. See 26.3.2.2.
        >[/color]

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

        john


        Comment

        Working...