Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About iterators in C++

Tags:

c++

iterator

stl

I read something from "More Effective C++", item 18:

In brief, the iterator it is an object, not a pointer, so there is no guarantee that “->” can be applied to it.† The STL does require that “.” and “*” be valid for iterators, however, so (*it).second, though syntactically clumsy, is guaranteed to work.)

Usually, I use -> on iterators and there isn't any problem. Could anyone give a example or explain it?

like image 561
frank.lin Avatar asked Dec 20 '22 11:12

frank.lin


1 Answers

"More Effective C++" is nearly 20 years old (published 1996); while an excellent book, it should be read with an awareness of its historical context.

In the 2003 version of the Standard (itself over 10 years old) iterators are required to support -> ([lib.iterator.requirements]/1):

[...] All iterators i for which the expression (*i).m is well-defined, support the expression i->m with the same semantics as (*i).m. [...]

You would only need to write (*i).m when working with an archaic implementation with non-compliant "standard" libraries.

like image 169
ecatmur Avatar answered Jan 02 '23 02:01

ecatmur