Using auto iterators in loop

Discussion in 'C++' started by Single Stage to Orbit, Aug 4, 2012.

  1. I've got an query about the following:

    for (auto iter = mEmployees.begin(); iter != mEmployees.end(); ++iter)
    iter->display();

    If I changed this to:

    for (auto iter : mEmployees)
    iter->display();

    GCC 4.6.3 complains about base operand of '->' having non-pointer type
    'Records::Empoyee' so what would I need to change to be able to call
    display()?
    --
    Tactical Nuclear Kittens
     
    Single Stage to Orbit, Aug 4, 2012
    #1
    1. Advertising

  2. Single Stage to Orbit <> writes:

    > I've got an query about the following:
    >
    > for (auto iter = mEmployees.begin(); iter != mEmployees.end(); ++iter)
    > iter->display();
    >
    > If I changed this to:
    >
    > for (auto iter : mEmployees)
    > iter->display();


    This form is designed to avoid the explicit use of iterators: the "loop
    variable" has the type of the elements of the container.

    > GCC 4.6.3 complains about base operand of '->' having non-pointer type
    > 'Records::Empoyee' so what would I need to change to be able to call
    > display()?


    [Please copy-paste error messages instead of retypîng them.]

    for (auto emp : mEmployees)
    emp.display();

    But you probably want a reference to avoid a copy, and maybe make it
    const if display() allows:

    for (const Records::Employee & emp : mEmployees)
    emp.display();

    (my personal feeling is that this looks better with an explicit type
    instead of auto). YMMV.

    -- Alain.
     
    Alain Ketterlin, Aug 4, 2012
    #2
    1. Advertising

  3. Single Stage to Orbit

    Melzzzzz Guest

    On Sat, 04 Aug 2012 14:15:07 +0200
    Alain Ketterlin <-strasbg.fr> wrote:

    > Single Stage to Orbit <> writes:
    >
    > > I've got an query about the following:
    > >
    > > for (auto iter = mEmployees.begin(); iter != mEmployees.end();
    > > ++iter) iter->display();
    > >
    > > If I changed this to:
    > >
    > > for (auto iter : mEmployees)
    > > iter->display();

    >
    > This form is designed to avoid the explicit use of iterators: the
    > "loop variable" has the type of the elements of the container.
    >
    > > GCC 4.6.3 complains about base operand of '->' having non-pointer
    > > type 'Records::Empoyee' so what would I need to change to be able
    > > to call display()?

    >
    > [Please copy-paste error messages instead of retypîng them.]
    >
    > for (auto emp : mEmployees)
    > emp.display();
    >
    > But you probably want a reference to avoid a copy, and maybe make it
    > const if display() allows:
    >
    > for (const Records::Employee & emp : mEmployees)
    > emp.display();
    >
    > (my personal feeling is that this looks better with an explicit type
    > instead of auto). YMMV.

    You could also capture auto by reference:

    for (const auto& emp : mEmployees)
    emp.display();
     
    Melzzzzz, Aug 4, 2012
    #3
  4. On Sat, 2012-08-04 at 14:15 +0200, Alain Ketterlin wrote:
    > > for (auto iter : mEmployees)
    > > iter->display();

    >
    > This form is designed to avoid the explicit use of iterators: the
    > "loop variable" has the type of the elements of the container.


    Exactly why I wanted to try it out. I think that C++ 11 is a massive
    improvement on older C++ revisions due to things like this.

    > > GCC 4.6.3 complains about base operand of '->' having non-pointer

    > type 'Records::Empoyee' so what would I need to change to be able to
    > call display()?
    >
    > [Please copy-paste error messages instead of retypîng them.]


    Next time I will.

    > for (auto emp : mEmployees)
    > emp.display();
    >
    > But you probably want a reference to avoid a copy, and maybe make it
    > const if display() allows:
    >
    > for (const Records::Employee & emp : mEmployees)
    > emp.display();
    >
    > (my personal feeling is that this looks better with an explicit type
    > instead of auto). YMMV.


    I've settled on this as I don't want to make a copy:

    for (auto& iter : mEmployees)
    iter.display();

    Iterators used needs the form '->' whilst that alternative form only
    needs a '.', thanks for clarifying that.

    Cheers
    Alex
    --
    Tactical Nuclear Kittens
     
    Single Stage to Orbit, Aug 4, 2012
    #4
  5. Single Stage to Orbit

    SG Guest

    Am Samstag, 4. August 2012 14:40:50 UTC+2 schrieb Single Stage to Orbit:
    >
    > I've settled on this as I don't want to make a copy:
    >
    > for (auto& iter : mEmployees)
    > iter.display();
    >
    > Iterators used needs the form '->' whilst that alternative form only
    > needs a '.', thanks for clarifying that.


    iter is not an iterator but a reference to one of your Employee objects.
    So, it makes much more sense to use something like "emp" instead of
    "iter" as its name.
     
    SG, Aug 4, 2012
    #5
  6. On Sat, 2012-08-04 at 07:28 -0700, SG wrote:
    > > Iterators used needs the form '->' whilst that alternative form only
    > > needs a '.', thanks for clarifying that.

    >
    > iter is not an iterator but a reference to one of your Employee
    > objects. So, it makes much more sense to use something like "emp"
    > instead of "iter" as its name.


    That does make sense, so thanks.

    for (const auto& emp : mEmployees)
    emp.display();
    Thanks!
    --
    Tactical Nuclear Kittens
     
    Single Stage to Orbit, Aug 4, 2012
    #6
    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. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    521
    Kai-Uwe Bux
    May 8, 2005
  2. Daniel Pitts
    Replies:
    14
    Views:
    1,282
    Patricia Shanahan
    Dec 23, 2006
  3. linkswanted
    Replies:
    1
    Views:
    996
  4. , India
    Replies:
    10
    Views:
    1,104
    James Kanze
    Aug 8, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    457
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page