iterator

Side effects in an iterator considered harmful?

I've written my first C# iterator today. Woohoo. Interestingly, it has side effects. My iterator filters out invalid files from a directory and returns a sequence of valid files to process. Wheneve it encounters an invlaid file, it moves it to another directory. I tried implementing it as a LINQ query, but really don't like the fact th...

Displaying dereferenced STL iterators in gdb

I have an iterator to a map element, and I would like gdb to show me the values of the "first" and "second" elements of that iterator. For example: std::map<int,double> aMap; ...fill map... std::map<int,double>::const_iterator p = aMap.begin(); I can use p.first and p.second in the code, but can't see them in gdb. For what it's worth,...

in python, is there a one line pythonic way to get a list of keys from a dictionary in sorted order?

The list sort method is a modifier function that returns None. So if I want to iterate through all of the keys in a dictionary I cannot do: for k in somedictionary.keys().sort(): dosomething() instead, i must: keys = somedictionary.keys() keys.sort() for k in keys: dosomething() Is there a pretty way to iterate through t...

In Python, is there a concise way to use a list comprehension with multiple iterators?

Basically, I would like to build a list comprehension over the "cartesian product" of two iterators. Think about the following Haskell code: [(i,j) | i <- [1,2], j <- [1..4]] which yields [(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4)] Can I obtain a similar behavior in Python in a concise way? ...

List iterator not incrementable error message in C++

Hi there, As a newbie I'm trying to implement a sorting function in C++, using the list-class. However, running the code I get the error that the list iterator is not incrementable... However it seems very unlikely as it should be incrementable! code: void shuffle (list<int> &list1) { list<int> smaller; list<int> larger; ...

Discarding the output of a function that needs an output iterator

Suppose there´s a template function in C++ that does some useful work but also outputs a sequence of values via an output iterator. Now suppose that that sequence of values sometimes is interesting, but at others is not useful. Is there a ready-to-use iterator class in the STL that can be instantiated and passed to the function and will ...

Python 3.0 - dict methods return views - why?

dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. http://docs.python.org/dev/3.0/whatsnew//3.0.html First of all how is a view different from an iterator? Secondly, what is the benefit of this change? Is it just for performance reasons? It doesn't seem intuitive to me, i.e., I'm ask...

Stuck on a Iterator Implementation of a Trie

Hello everyone, I have to implement a homemade Trie and I'm stuck on the Iterator part. I can't seem to figure out the increment method for the trie. I hope someone can help me clear things out. Here's the code for the Iterator: template <typename T> class Trie<T>::IteratorPrefixe{ friend class Trie<T>; public: IteratorPrefixe() ...

Is there a dereference_iterator in the STL?

I was wondering if there is an iterator in the STL that dereferences the object pointed before returning it. This could be very useful when manipulating containers aggregating pointers. Here's an example of what I would like to be able to do: #include <vector> #include <iterator> #include <algorithm> using namespace std; int main() { ...

Which implementation of Iterator should I use in PHP, and why?

I'm trying to refactor a large, old project and one thing I've noticed is a range of different Iterator implementations: while($iterator->moveNext()) { $item = $iterator->current(); // do something with $item; } for($iterator = getIterator(), $iterator->HasNext()) { $item = $iterator->Next(); // do something with $it...

Shortest way to get an Iterator over a range of Integers in Java

What's the shortest way to get an Iterator over a range of Integers in Java? In other words, implement the following: /** * Returns an Iterator over the integers from first to first+count. */ Iterator<Integer> iterator(Integer first, Integer count); Something like (first..first+count).iterator() ...

Help with C++ List erase function

I'm trying to do a simple erase and keep getting errors. Here is the snippet of code for my erase: std::list<Mine*>::iterator iterMines = mines.begin(); for(int i = oldSizeOfMines; i >0 ; i--, iterMines++) { if(player->distanceFrom(*iterMines) < radiusOfOnScreen) { onScreen.push_back(*iterMines); iterMines = onScreen.erase(iterMi...

Finding the owner of an STL iterator

Is there any way that I can find the container pointed to by an iterator? Specifically, I want to be able to find the std::vector pointed to by a particular std::vector::iterator so that I can check the range, without having to actually pass references to that vector around. If (as I suspect) the answer is no, why not? edit: thanks fo...

How to iterate over a list repeating each element in Python

I'm using Python to infinitely iterate over a list, repeating each element in the list a number of times. For example given the list: l = [1, 2, 3, 4] I would like to output each element two times and then repeat the cycle: 1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2 ... I've got an idea of where to start: def cycle(iterable): if not has...

Yield keyword value added?

still trying to find where i would use the "yield" keyword in a real situation. I see this thread on the subject http://stackoverflow.com/questions/39476/what-is-the-yield-keyword-used-for-in-c but in the accepted answer, they have this as an example where someone is iterating around Integers() public IEnumerable<int> Integers() { y...

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

For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. One quick way is to do this: lines = len(list(open(fname))) However, this loads the whole file into memory (at once). This rather defeats the purpose of an iterator (which only needs to keep the current line in...

How to iterate through a tree structure using a generator?

Hello people. I'm trying to figure out how to implement a function in a tree node which returns all its descendant leaves (whether direct or indirect). However, I don't want to pass a container in which the leaf nodes will be put recursively (the tree could be huge), instead I'd like to use a generator to iterate through the tree. I've ...

How do I erase a reverse_iterator from an stl data structure?

For some reason the following code fails. You can't simply erase a reverse_iterator by using its base() method. #include <set> #include <iostream> int main() { std::set<int> setOfInts; setOfInts.insert(1); setOfInts.insert(2); setOfInts.insert(3); std::set<int>::reverse_iterator rev_iter = setOfInts.rbegin(); ...

Is there any way to get all the controls on a container control?

I've got a form with a bunch of controls on it, and I wanted to iterate through all the controls on a certain panel and enable/disable them. I tried this: var component: TComponent; begin for component in myPanel do (component as TControl).Enabled := Value; end; But that did nothing. Turns out all components are in the form's ...

Is there any difference between iterator methods returning IEnumerable<T> and IEnumerator<T>?

Consider two iterator methods with the same bodies: public static IEnumerable<int> It1() { ... } public static IEnumerator<int> It2() { ... } Is there any circumstance where calling It2 is different from calling It1.GetEnumerator()? Is there ever a good reason to define an iterator as IEnumerator<T> over IEnumerable<T>? Th...