iterator

Java: how to get Iterator<Character> from String

Hi, I need a Iterator<Character> from a String object. Is there any available function in Java that provides me this or do I have to code my own? ...

Iterator (iter()) function in Python.

For dictionary, I can use iter() for iterating over keys of the dictionary. y = {"x":10, "y":20} for val in iter(y): print val When I have the iterator as follows, class Counter: def __init__(self, low, high): self.current = low self.high = high def __iter__(self): return self def next(self):...

Java Iterate Over Collection

I have a practice project which I need help with. It's a simple MailServer class. Here's the code: import java.util.ArrayList; import java.util.List; import java.util.Iterator; import java.util.HashMap; import java.util.TreeMap; import java.util.Collection; import java.util.Map; public class MailServer { private HashMap<String, Arr...

Python filter / max combo - checking for empty iterator

(Using Python 3.1) I know this question has been asked many times for the general question of testing if iterator is empty; obviously, there's no neat solution to that (I guess for a reason - an iterator doesn't really know if it's empty until it's asked to return its next value). I have a specific example, however, and was hoping I ca...

exhausted iterators - what to do about them?

(In Python 3.1) (Somewhat related to another question I asked, but this question is about iterators being exhausted.) # trying to see the ratio of the max and min element in a container c filtered = filter(lambda x : x is not None and x != 0, c) ratio = max(filtered) / min(filtered) It took me half hour to realize what the problem is ...

Java iterators and for-each-loop. any way to access the underlying iterator?

I very much like the for-each-loop construction (for(T e : iterable)) in Java which works on any Iterable<T> because it makes in many cases very easy to read and to write code. I wonder though if there is any way that I can access the underlying iterator from such a loop. This can be mandatory if I want to use the remove() from the iter...

Best style for iterating through two lists in unison

Here is what I just wrote: public void mutate(){ ListIterator<Double> git = genome.listIterator(); Iterator<Double> mit = mutationStrategies.iterator(); while (git.hasNext() && mit.hasNext()){ git.set(alleleUpdate(git.next(), mit.next())); } } Is this the most efficient and clearest way of doing that? All tha...

python: how to slice/store iter pointed data in a fixed buffer class?

All, As you know, by python iter we can use iter.next() to get the next item of data. take a list for example: l = [x for x in range(100)] itl = iter(l) itl.next() # 0 itl.next() # 1 Now I want a buffer can store *general iter pointed data * slice in fixed size, use above list iter to demo my question. class I...

how itertools.tee works, can type 'itertools.tee' be duplicated in order to save it's "status" ?

All, pls see below test code about itertools.tee: li = [x for x in range(10)] ite = iter(li) ================================================== it = itertools.tee(ite, 5) >>> type(ite) <type 'listiterator'> >>> type(it) <type 'tuple'> >>> type(it[0]) <type 'itertools.tee'> >>> >>> list(ite) ...

Why is there no "Iterable" interface in the STL?

The C++ STL does not seem to use purely abstract base classes (aka interfaces) very often. I know that most things can be achieved with the STL algorithms or clever template metaprogramming. But still, for some use cases (for example, in an API, if I do not want to be specific about the type of container I get, just about the elements ...

Fast and flexible iterator for abstract class

In order to traverse grids with data in a fast and flexible way I set up an abstract, templated GridDataStructure class. The data should be accessed by STL iterators. When someone uses the class, he should not worry about which kind of STL iterator is appropriate for a specific subclass. A solution to this problem seems to be http://st...

java: reverse list

I want to have a reversed list view on a list (in a similar way than List#sublist provides a sublist view on a list). Is there some function which provides this functionality? I don't want to make any sort of copy of the list nor modify the list. It would be enough if I could get at least a reverse iterator on a list in this case thoug...

How to retrieve value type from iterator in C++?

Hi everyone, my question is sure a simple one for anybody familiar with C++ syntax. I'm just learning c++ and this is some sort of homework. template<typename Iter> void quickSort(Iter begin, Iter end) { //.. auto pivot = * ( begin + (end - begin)/2 ); //.. } pivot is supposed to contain value from the center of [b...

Interface-based programming in C++ in combination with iterators. How too keep this simple?

In my developments I am slowly moving from an object-oriented approach to interface-based-programming approach. More precisely: in the past I was already satisfied if I could group logic in a class now I tend to put more logic behind an interface and let a factory create the implementation A simple example clarifies this. In the pa...

What is the most elegant way to loop TWICE in C

Many times I need to do things TWICE in a for loop. Simply I can set up a for loop with an iterator and go through it twice. for (i = 0; i < 2; i++) { // Do stuff } Now I am interested in doing this as SIMPLY as I can, perhaps without an initializer or iterator ... or any really simple and elegant way ... How would you do it? x ...

std::vector iterator incompatibles

I have an error (vector iterator incompatibles) during execution in my C++ program that I do not understand. [ (Windows / Visual C++ 2008 Express) ] Here is a simplified version of my problem : #include <vector> class A { int mySuperInt; public: A(int val) : mySuperInt(val) {} }; class B { std::vector<A*> myAs; public:...

Is there an is_iterable or similar function for PHP?

Hello, I am passing a JSON'd feed from Twitter into my application, and sometimes Twitters API returns empty or incomplete feeds. This is causing a problem when I try to iterate the feeds using foreach - does anyone know of a suitable test to find out if an object or array is iterable? Could this be done with isarray($obj) || $obj inst...

How is *it++ valid for output iterators?

In example code, I often see code such as *it++ for output iterators. The expression *it++ makes a copy of it, increments it, and then returns the copy which is finally dereferenced. As I understand it, making a copy of an output iterator invalidates the source. But then the increment of it that is performed after creating the copy would...

C++ Iterate an istream

What is the best way to parse or iterate an istream? I need to create a function that takes an istream, parses it and creates an object so was wondering the easiest way to do this. Even something that could convert it to string would be dandy. ...

back_insert_iterator with remove_copy_if

I am trying to use a back_insert_iterator with remove_copy_if using vectors but I have compile errors. Do you know why the code below is wrong? #include <iostream> #include <string> #include <algorithm> #include <cassert> #include <vector> #include <iterator> #include <functional> struct checkRem : std::unary_function<int,bool> { ...