Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using auto iterators in loop

Reply
Thread Tools

Using auto iterators in loop

 
 
Single Stage to Orbit
Guest
Posts: n/a
 
      08-04-2012
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

 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      08-04-2012
Single Stage to Orbit <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Melzzzzz
Guest
Posts: n/a
 
      08-04-2012
On Sat, 04 Aug 2012 14:15:07 +0200
Alain Ketterlin <(E-Mail Removed)-strasbg.fr> wrote:

> Single Stage to Orbit <(E-Mail Removed)> 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();



 
Reply With Quote
 
Single Stage to Orbit
Guest
Posts: n/a
 
      08-04-2012
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

 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      08-04-2012
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.
 
Reply With Quote
 
Single Stage to Orbit
Guest
Posts: n/a
 
      08-04-2012
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

 
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
Auto Shipping Auto Shipping Scheduling:car moving auto transport linkswanted ASP .Net 1 11-22-2013 07:02 AM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Opinion poll: for loop vs while loop with Iterators. Daniel Pitts Java 14 12-23-2006 05:22 AM
Iterators and reverse iterators Marcin Kaliciski C++ 1 05-08-2005 09:58 AM



Advertisments