Can we write our own iterator in Java?

Sure. An iterator is just an implementation of the java.util.Iterator interface. If you’re using an existing iterable object (say, a LinkedList) from java.util, you’ll need to either subclass it and override its iterator function so that you return your own, or provide a means of wrapping a standard iterator in your special Iterator instance (which has the advantage of being more broadly used), … Read more

why can’t I dereference an iterator?

They are three separate errors: won’t work because end() returns a an iterator, and object is a pointer. Those are generally distinct types (A vector can use raw pointers as iterators, but not all implementations do it. Other containers require special iterator types). doesn’t work because you take the address of the returned iterator. That … Read more

How to iterate through two lists in parallel?

Python 3 zip stops when the shorter of foo or bar stops. In Python 3, zip returns an iterator of tuples, like itertools.izip in Python2. To get a list of tuples, use list(zip(foo, bar)). And to zip until both iterators are exhausted, you would use itertools.zip_longest. Python 2 In Python 2, zip returns a list of tuples. This is fine when foo and bar are not massive. If they are both massive then forming zip(foo,bar) is … Read more

How to iterate over a vector?

If you have access to C++11 you can use range-based for loops Otherwise you should use begin() and end() You can also use std::begin and std::end (these require C++11 as well) begin will return an iterator to the first element in your vector. end will return an iterator to one element past the end of your vector. So the order in which you get the elements iterating … Read more

Why use string::iterator rather than index?

The index can only be used for containers that support random access – direct access to a given position. The iterator offers a unified way to access any collection/data structure. The flexibility when refactoring your code is immense.