Iterators in C++ (stl) vs Java, is there a conceptual difference?

前端 未结 9 996
夕颜
夕颜 2020-12-13 09:30

I\'m returning to c++ after being away for a bit and trying to dust off the old melon.

In Java Iterator is an interface to a container having methods: hasNext

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 09:50

    A pointer to an array element is indeed an iterator into the array.

    As you say, in Java, an iterator has more knowledge of the underlying container than in C++. C++ iterators are general, and a pair of iterators can denote any range: this can be a sub-range of a container, a range over multiple containers (see http://www.justsoftwaresolutions.co.uk/articles/pair_iterators.pdf or http://www.boost.org/doc/libs/1_36_0/libs/iterator/doc/zip_iterator.html) or even a range of numbers (see http://www.boost.org/doc/libs/1_36_0/libs/iterator/doc/counting_iterator.html)

    The iterator categories identify what you can and can't do with a given iterator.

提交回复
热议问题