Using auto iterators in loop

  • Thread starter Single Stage to Orbit
  • Start date
S

Single Stage to Orbit

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()?
 
A

Alain Ketterlin

Single Stage to Orbit said:
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.
 
M

Melzzzzz

Single Stage to Orbit said:
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();
 
S

Single Stage to Orbit

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
 
S

SG

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.
 
S

Single Stage to Orbit

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top