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

前端 未结 9 949
夕颜
夕颜 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条回答
  • 2020-12-13 10:05

    As mentioned, Java and C# iterators describe an intermixed position(state)-and-range(value), while C++ iterators separate the concepts of position and range. C++ iterators represent 'where am I now' separately from 'where can I go?'.

    Java and C# iterators can't be copied. You can't recover a previous position. The common C++ iterators can.

    Consider this example:

    // for each element in vec
    for(iter a = vec.begin(); a != vec.end(); ++a){
      // critical step!  We will revisit 'a' later.
      iter cur = a; 
      unsigned i = 0;
      // print 3 elements
      for(; cur != vec.end() && i < 3; ++cur, ++i){
          cout << *cur << " ";
      }
      cout << "\n";
    }
    

    Click the above link to see program output.

    This rather silly loop goes through a sequence (using forward iterator semantics only), printing each contiguous subsequence of 3 elements exactly once (and a couple shorter subsequences at the end). But supposing N elements, and M elements per line instead of 3, this algorithm would still be O(N*M) iterator increments, and O(1) space.

    The Java style iterators lack the ability to store position independently. You will either

    • lose O(1) space, using (for example) an array of size M to store history as you iterate
    • will need to traverse the list N times, making O(N^2+N*M) time
    • or use a concrete Array type with GetAt member function, losing genericism and the ability to use linked list container types.

    Since only forward iteration mechanics were used in this example, i was able to swap in a list with no problems. This is critical to authoring generic algorithms, such as search, delayed initialization and evaluation, sorting, etc.

    The inability to retain state corresponds most closely to the C++ STL input iterator, on which very few algorithms are built.

    0 讨论(0)
  • 2020-12-13 10:08

    There are plenty of good answers about the differences, but I felt the thing that annoys me the most with Java iterators wasn't emphasized--You can't read the current value multiple times. This is really useful in a lot of scenarios, especially when you are merging iterators.

    In c++, you have a method to advance the iterator and to read the current value. Reading its value doesn't advance the iteration; so you can read it multiple times. This is not possible with Java iterators, and I end up creating wrappers that do this.

    A side note: one easy way to create a wrapper is to use an existing one--PeekingIterator from Guava.

    0 讨论(0)
  • 2020-12-13 10:08

    Iterators are only equivalent to pointers in the trivial case of iterating over the contents of an array in sequence. An iterator could be supplying objects from any number of other sources: from a database, from a file, from the network, from some other calculation, etc.

    0 讨论(0)
提交回复
热议问题