tagged [iterator]

Why does Enumerable.Range Implement IDisposable?

Why does Enumerable.Range Implement IDisposable? Just wondering why `Enumerable.Range` implements `IDisposable`. I understand why `IEnumerator` does, but `IEnumerable` doesn't require it. --- (I disco...

C++ STL Vectors: Get iterator from index?

C++ STL Vectors: Get iterator from index? So, I wrote a bunch of code that accesses elements in an stl vector by index[], but now I need to copy just a chunk of the vector. It looks like `vector.inser...

25 May at 09:28

Asynchronous iterator Task<IEnumerable<T>>

Asynchronous iterator Task> I’m trying to implement an asynchronous function that returns an iterator. The idea is the following: However, there is an error messag

25 Apr at 13:51

How to navigate through a vector using iterators? (C++)

How to navigate through a vector using iterators? (C++) The goal is to access the "nth" element of a vector of strings instead of the [] operator or the "at" method. From what I understand, iterators ...

7 Mar at 05:44

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

How does the enhanced for statement work for arrays, and how to get an iterator for an array? Given the following code snippet: I have the following questions: 1. How does the above for-each loop work...

26 Jul at 14:16

Is it possible to do start iterating from an element other than the first using foreach?

Is it possible to do start iterating from an element other than the first using foreach? I'm thinking about implementing IEnumerable for my custom collection (a tree) so I can use foreach to traverse ...

7 May at 23:31

What does iterator->second mean?

What does iterator->second mean? In C++, what is the type of a `std::map::iterator`? We know that an object `it` of type `std::map::iterator` has an overloaded `operator ->` which returns a `std::pair...

27 Jan at 21:8

What's the best way to iterate over two or more containers simultaneously

What's the best way to iterate over two or more containers simultaneously C++11 provides multiple ways to iterate over containers. For example: ## Range-based loop ## std::for_each However what is the...

25 Sep at 13:9

When should I use IEnumerator for looping in c#?

When should I use IEnumerator for looping in c#? I was wondering if there are any times where it's advantageous to use an IEnumerator over a foreach loop for iterating through a collection? For exampl...

19 Jan at 03:19

How to pick just one item from a generator?

How to pick just one item from a generator? I have a generator function like the following: The usual way to call this function would be: My question, is there a way to get just one element from the g...

30 Jun at 00:16

Java foreach loop: for (Integer i : list) { ... }

Java foreach loop: for (Integer i : list) { ... } When I use JDK5 like below on the other hand if I just use an `Iterator` ``` ArrayList list = new ArrayList(); for (Iterator i = list.iterator(); i.h...

7 Nov at 16:31

What's the use of yield break?

What's the use of yield break? > [What does “yield break;” do in C#?](https://stackoverflow.com/questions/231893/what-does-yield-break-do-in-c) Can anyone see a use for the "yield break" statement t...

23 May at 10:31

What is the purpose/advantage of using yield return iterators in C#?

What is the purpose/advantage of using yield return iterators in C#? All of the examples I've seen of using `yield return x;` inside a C# method could be done in the same way by just returning the who...

6 Jul at 18:11

Calling remove in foreach loop in Java

Calling remove in foreach loop in Java In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance: As an addendum, is it legal to remo...

30 Jul at 20:16

Best way to loop over a python string backwards

Best way to loop over a python string backwards What is the best way to loop over a python string backwards? The following seems a little awkward for all the need of -1 offset: The following seems mor...

How to iterate through a list of objects in C++?

How to iterate through a list of objects in C++? I'm very new to C++ and struggling to figure out how I should iterate through a list of objects and access their members. I've been trying this where `...

28 May at 03:16

Clever Uses of .Net 2 Iterators

Clever Uses of .Net 2 Iterators C# 2 and VB.Net 8 introduced a new feature called [iterators](http://msdn.microsoft.com/en-us/library/dscyy5s0.aspx), which were designed to make it easier to return en...

22 Jan at 01:1

Is if(items != null) superfluous before foreach(T item in items)?

Is if(items != null) superfluous before foreach(T item in items)? I often come across code like the following: Basically, the `if` condition ensures that `foreach` block will execute only if `items` i...

7 Oct at 19:33

java- reset list iterator to first element of the list

java- reset list iterator to first element of the list I need to know how to "reset" LinkedList iterator to its first element. For example: Over and over again and after many moves of the iterator, I ...

6 Apr at 17:39

Iterator blocks and inheritance

Iterator blocks and inheritance Given a base class with the following interface: I want to make a derived class that overrides the method, and adds its own stuff, like so: ``` public class Derived : B...

12 Mar at 13:1

Returning a pointer to a vector element in c++

Returning a pointer to a vector element in c++ I have a vector of myObjects in global scope. I have a method which uses a `std::vector::const_iterator` to traverse the vector, and doing some compariso...

26 Mar at 16:43

Python range() and zip() object type

Python range() and zip() object type I understand how functions like `range()` and `zip()` can be used in a for loop. However I expected `range()` to output a list - much like `seq` in the unix shell....

27 Aug at 23:24

C#: yield return within a foreach fails - body cannot be an iterator block

C#: yield return within a foreach fails - body cannot be an iterator block Consider this bit of obfuscated code. The intention is to create a new object on the fly via the anonymous constructor and `y...

10 Mar at 01:12

How to iterate over two arrays at once?

How to iterate over two arrays at once? I have two arrays built while parsing a text file. The first contains the column names, the second contains the values from the current row. I need to iterate o...

30 Jan at 18:50

Recursion in C# iterator

Recursion in C# iterator Is it possible to use recursion in an iterator implementing `System.Collections.IEnumerable`? I have a tree structure declared roughly like this: I would like to iterate over ...

17 Nov at 06:36