【发布时间】:2012-03-31 23:43:22
【问题描述】:
是否有一种“计算上”的快速方法来获取迭代器的计数?
int i = 0;
for ( ; some_iterator.hasNext() ; ++i ) some_iterator.next();
... 似乎是在浪费 CPU 周期。
【问题讨论】:
-
迭代器不一定对应于带有“计数”的东西...
-
迭代器就是它们的样子;迭代到集合的下一个对象(它可以是集合、数组等任何东西)。为什么当他们不关心他们试图迭代的内容时,他们需要告诉大小?
to provide an implementation-independent method for access, in which the user does not need to know whether the underlying implementation is some form of array or of linked list, and allows the user go through the collection without explicit indexing.penguin.ewu.edu/~trolfe/LinkedSort/Iterator.html