简体   繁体   中英

About iterators in C++

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM