gpt4 book ai didi

c++ - 关于 C++ 中的迭代器

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:10 26 4
gpt4 key购买 nike

我从“更有效的 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.)

通常,我在迭代器上使用->,没有任何问题。谁能举个例子或解释一下?

最佳答案

“更有效的 C++”已有将近 20 年的历史(1996 年出版);虽然是一本优秀的书,但阅读它时应了解其历史背景。

在 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

关于c++ - 关于 C++ 中的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25638448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com