How does the enhanced for statement work for arrays, and how to get an iterator for an array?

If you want an Iterator over an array, you could use one of the direct implementations out there instead of wrapping the array in a List. For example: Apache Commons Collections ArrayIterator Or, this one, if you’d like to use generics: com.Ostermiller.util.ArrayIterator Note that if you want to have an Iterator over primitive types, you … Read more

What is an iterator’s default value?

By convention a “NULL iterator” for containers, which is used to indicate no result, compares equal to the result of container.end(). std::vector<X>::iterator iter = std::find(my_vec.begin(), my_vec.end(), x); if (iter == my_vec.end()) { //no result found; iter points to “nothing” } However, since a default-constructed container iterator is not associated with any particular container, there is … Read more

What happens if you increment an iterator that is equal to the end iterator of an STL container

Following is the quote from Nicolai Josuttis book: Note that advance() does not check whether it crosses the end() of a sequence (it can’t check because iterators in general do not know the containers on which they operate). Thus, calling this function might result in undefined behavior because calling operator ++ for the end of … Read more

Enumerate properties on an object

You have two options, using the Object.keys() and then forEach, or use for/in: class stationGuide { station1: any; station2: any; station3: any; constructor(){ this.station1 = null; this.station2 = null; this.station3 = null; } } let a = new stationGuide(); Object.keys(a).forEach(key => console.log(key)); for (let key in a) { console.log(key); } (code in playground)

Why does defining __getitem__ on a class make it iterable in python?

Iteration’s support for __getitem__ can be seen as a “legacy feature” which allowed smoother transition when PEP234 introduced iterability as a primary concept. It only applies to classes without __iter__ whose __getitem__ accepts integers 0, 1, &c, and raises IndexError once the index gets too high (if ever), typically “sequence” classes coded before __iter__ appeared … Read more

Preparation for std::iterator Being Deprecated

The discussed alternatives are clear but I feel that a code example is needed. Given that there will not be a language substitute and without relying on boost or on your own version of iterator base class, the following code that uses std::iterator will be fixed to the code underneath. With std::iterator template<long FROM, long … Read more

Distinction between iterator and enumerator

Iterating means repeating some steps, while enumerating means going through all values in a collection of values. So enumerating usually requires some form of iteration. In that way, enumerating is a special case of iterating where the step is getting a value from a collection. Note the “usually” – enumerating may also be performed recursively, … Read more

Is there any built-in way to get the length of an iterable in python?

Short of iterating through the iterable and counting the number of iterations, no. That’s what makes it an iterable and not a list. This isn’t really even a python-specific problem. Look at the classic linked-list data structure. Finding the length is an O(n) operation that involves iterating the whole list to find the number of … Read more

Why does Java not allow foreach on iterators (only on iterables)? [duplicate]

So I have a somewhat reasonable explanation now: Short version: Because the syntax also applies to arrays, which don’t have iterators. If the syntax were designed around Iterator as I proposed, it would be inconsistent with arrays. Let me give three variants: A) as chosen by the Java developers: Object[] array; for(Object o : array) … Read more

What is the difference between cbegin and begin for vector?

begin will return an iterator or a const_iterator depending on the const-qualification of the object it is called on. cbegin will return a const_iterator unconditionally. std::vector<int> vec; const std::vector<int> const_vec; vec.begin(); //iterator vec.cbegin(); //const_iterator const_vec.begin(); //const_iterator const_vec.cbegin(); //const_iterator