简体   繁体   English

关于C ++中的迭代器

[英]About iterators in C++

I read something from "More Effective C++", item 18: 我从“更有效的C ++”第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.) 简而言之,迭代器是一个对象,而不是一个指针,因此无法保证“ - >”可以应用于它。†STL确实要求“。”和“*”对迭代器有效,但是,所以(*它)。第二,尽管语法上很笨拙,但保证可以工作。)

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); “更有效的C ++”已有近20年的历史(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): 在2003版本的标准(本身超过10年)中,迭代器需要支持->[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. [...]表达式(*i).m定义明确的所有迭代器i支持表达式i->m ,其语义与(*i).m. [...] [...]

You would only need to write (*i).m when working with an archaic implementation with non-compliant "standard" libraries. 在使用不符合“标准”库的古老实现时,您只需要编写(*i).m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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