iterator

C++ explicit constructors and iterators

Consider the following code: #include <vector> struct A { explicit A(int i_) : i(i_) {} int i; }; int main() { std::vector<int> ints; std::vector<A> As(ints.begin(), ints.end()); } Should the above compile? My feeling is that it should not, due to the constructor being marked explicit. Microsoft Visual C++ agrees, g...

php: polymorphism on the iterator implementation of a array object?

I don't know if a array is really a object on php ... but, what I want is to change the array behavior on a foreach loop. Something similar to this on java: for( String it : myArr = new Array_Iterator( array) implements Iterator{ public Array_Iterator( String[] arr){ this.arr = arr} /* interface implementation */ }){ /* loo...

Alternating between iterators in Python

What is the most efficient way to alternate taking values from different iterators in Python, so that, for example, alternate(xrange(1, 7, 2), xrange(2, 8, 2)) would yield 1, 2, 3, 4, 5, 6. I know one way to implement it would be: def alternate(*iters): while True: for i in iters: try: yield i.nex...

How do i peek at at the next value of string iterator

In a loop running over the entire string how do i peek at the next value of iterator? for (string::iterator it = inp.begin(); it!= inp.end(); ++it) { // Just peek at the next value of it, without actually incrementing the iterator } This is quite simple in C, for (i = 0; i < strlen(str); ++i) { if (str[i] == str[i+1]) { ...

Hashmap.keySet(), foreach, and remove

I know that it's typically a big no-no to remove from a list using java's "foreach" and that one should use iterator.remove(). But is it safe to remove() if I'm looping over a HashMap's keySet()? Like this: for(String key : map.keySet()) { Node n = map.get(key).optimize(); if(n == null) { map.remove(key); } else { map.put(...

Construct an Iterator

Let's say you want to construct an Iterator that spits out File objects. What type of data do you usually provide to the constructor of such an Iterator? an array of pre-constructed File objects, or simply raw data (multidimensional array for instance), and let the Iterator create File objects on the fly when Iterated through? Edit: ...

How to iterate a class of my creation in Java?

I created a class MyList that has a field private LinkedList<User> list; I would like to be able to iterate the list like this: for(User user : myList) { //do something with user } (when my list is an instance of MyList). How? What should I add to my class? ...

Can I convert a reverse iterator to a forward iterator?

Hi, I have a class called Action, which is essentially a wrapper around a deque of Move objects. Because I need to traverse the deque of Moves both forward and backwards, I have a forward iterator and a reverse_iterator as member variables of the class. The reason for this is becuase I need to know when I have gone one past the "end" ...

Is storing iterators inside this class unwise? How else to iterate through this sequence?

Hi, Warning this is a long question! I am implementing a Solitaire card game in C++ on Win32, and after asking this question, it's becoming clear that I may need a bit of guidance regarding actual class design rather than implementation details. I am using a model view Controller pattern to implement the game. The model is the game, c...

round robin scheduling java iterators

I have a list of hosts in an array which represnt the servers available to do a particular job. Currently I simply iterate thru the list looking and establish comms with a host to check its not busy. If not I will send a job to it. This approach tends to mean that the first host in the list tends to get hot constanly with the load not ba...

Qt 4.6 - C++ - QTreeWidgetItem Iterator

Hello everyone! I have a QTreeWidget with items in it. The first column contains a unique number. This is set via item->setData(0, 0, unique_number);. The second column contains a checkbox, set via item->setCheckState(1, Qt::Unchecked);. The user can select the items (s)he would like to work with and presses a button. The slot for this b...

How to extend a binary search iterator to consume multiple targets

I have a function, binary_range_search, that is called like so: my $brs_iterator = binary_range_search( target => $range, # eg. [1, 200] search => $ranges # eg. [ {start => 1, end => 1000}, ); # {start => 500, end => 1500} ] brs_iterator->() will ...

How to create an iterable wrapper for TreeMap and HashMap (Java)?

I have a class MyMap which wraps TreeMap. (Say it's a collection of dogs and that the keys are strings). public class MyMap { private TreeMap<String, Dog> map; ... } I would like to turn MyMap iterable with the for-each loop. I know how I would've done it if my class was a LinkedList wrapper: public class MyList implements Iterabl...

How do you interate over a Collection<T> and modify its items without ConcurrentModificationException?

I need to do something like this... Collection<T> myCollection; ///assume it is initialized and filled for(Iterator<?> index = myCollection.iterator(); index.hasNext();) { Object item = index.next(); myCollection.remove(item); } Obviously this throws ConcurrentModificationException... So I have tried this but doesn't does s...

C++ Best way to check if an iterator is valid

Hi! Another newbie question: Is there any way to check if a iterator (whether it is from a vector, a list, a deque...) is (still) dereferencable, i.e has not been invalidated ? If so, which is the best way, in your opinion? (I was 'trying' and 'catching' :/) Thanks again! Edit: An example (which doesn't work) list<int>l; for (i=1; i<...

Is there any reason that the STL does not provide functions to return an iterator via index?

Hi, Is there a reason that the STL does not provide functions to return an iterator into a container via an index? For example, let's say I wanted to insert an element into a std::list but at the nth position. It appears that I have to retrieve an iterator via something like begin() and add n to that iterator. I'm thinking it would be ...

Returning from a multimap search with equal_range without being error-prone

I'm about to refactor some duplicated code. Two functions both search in a multimap using equal_range(). In a for loop after the call to equal_range() there is a for loop that sets an iterator to equalRange.first with the condition it != equalRange.second. If the correct value is found, the two functions differ. What I would like to do...

In a C++ template function can I return a dereferenced argument type ?

What I mean is the following. I want a template function that takes two vector iterators (or two pointers to array of double) and returns a double that is somehow related to the vector iterators or array pointers that I pass. However, I want this to work for double or int, or any arithmetic type. I think I'm not allowed to say: templat...

Why is there a method iterator() on java.util.Collection

Why is there the method iterator() defined on the interface java.util.Collection when it already extends java.util.Iterable which has this very method defined. I'm thinking some sort of backward compatability or an opportunity to write some JavaDoc on the method at the collection level. Any other ideas? ...

the patterns used in iterators

I am familiar with the usage of C++ STL iterators, e.g. for(map<pair<int,int>>::iterator it=m.begin(); it!=m.end(); ++it) int a = it->first; int b = it->second; But I don't know the inner details in it. Could some explain to me? Either in C++, Java, C# or Python. ...