std::uninitialized_fill

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

    std::uninitialized_fill

    Simple newbie question:

    I want to set an int array to value 5

    int i[3];
    int val=5;
    std::uninitiali zed_fill(&i[0], &i[3], val);
    OR
    std::uninitiali zed_fill(i, i+3, val);

    I need to pass as last iterator an out of index reference to completly fill
    the array.
    I think because std::uninitiali zed_fill do something like "while ( First !=
    Last ) {...do something...}"
    So my iterator will be incremented out of index? Is it dangerous?

    Many thanks

    Michele


  • JE

    #2
    Re: std::uninitiali zed_fill

    Michele Moccia wrote:[color=blue]
    > Simple newbie question:
    >
    > I want to set an int array to value 5
    >
    > int i[3];
    > int val=5;[/color]

    <snip>

    std::fill( i, i + sizeof(i)/sizeof(*i), val );

    Leave uninitialized_f ill alone, unless you're writing allocators or
    something...

    Comment

    • Dietmar Kuehl

      #3
      Re: std::uninitiali zed_fill

      Michele Moccia wrote:[color=blue]
      > I want to set an int array to value 5
      >
      > int i[3];
      > int val=5;
      > std::uninitiali zed_fill(&i[0], &i[3], val);
      > OR
      > std::uninitiali zed_fill(i, i+3, val);[/color]

      .... or:

      template <typename T, int size>
      T* begin(T(&array)[size]) { return array; }
      template <typename T, int size>
      T* end(T(&array)[size]) { return array + T; }

      int main()
      {
      int i[3];
      std::fill(begin (i), end(i), 5);
      }
      [color=blue]
      > I need to pass as last iterator an out of index reference to completly
      > fill the array.[/color]

      The pointer to the element one past the last array element is always
      defined. Thus, it is safe to pass this pointer as an argument to a
      function and, in fact, the STL algorithms take heavy advantage of
      this features. Note, however, that you may not dereference this
      pointer!
      --
      <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
      <http://www.eai-systems.com> - Efficient Artificial Intelligence

      Comment

      • JE

        #4
        Re: std::uninitiali zed_fill

        Michele Moccia wrote:[color=blue]
        > Simple newbie question:
        >
        > I want to set an int array to value 5
        >
        > int i[3];
        > int val=5;[/color]

        <snip>

        std::fill( i, i + sizeof(i)/sizeof(*i), val );

        Leave uninitialized_f ill alone, unless you're writing allocators or
        something...

        Comment

        • red floyd

          #5
          Re: std::uninitiali zed_fill

          Dietmar Kuehl wrote:[color=blue]
          > [redacted
          > template <typename T, int size>
          > T* end(T(&array)[size]) { return array + T; }[/color]
          I assume you mean { return array + size; }[color=blue]
          >[/color]

          Comment

          • Dietmar Kuehl

            #6
            Re: std::uninitiali zed_fill

            red floyd wrote:
            [color=blue]
            > Dietmar Kuehl wrote:[color=green]
            >> [redacted
            >> template <typename T, int size>
            >> T* end(T(&array)[size]) { return array + T; }[/color]
            > I assume you mean { return array + size; }[/color]

            Yes, of course: I just typed the code without testing it...
            --
            <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
            <http://www.eai-systems.com> - Efficient Artificial Intelligence

            Comment

            Working...