iterator

Java Set iterator, safe for removal of elements?

I would like to iterate over a Set and remove the elements from the set that match some condition. The documentation of iterator says nothing about modifying the list while iterating over it. Is this possible? If not, what would be the best way to do it? Note that I only want to remove elements from the set that are provided by the Ite...

RecursiveIteratorIterator last child

I iterate through a multidimensional array with RecursiveIteratorIterator and would like to be able to know if the current element is the last child of it's depth. I thought about this: $iterator = new RecursiveIteratorIterator($array, RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $val) { $next = clone $iterator;...

(C++) Extending vector iterator to fill my needs.

Hi everoby, I have the following design in one of my projects: template<typename C> class B { int numValue; C inner; } template<typename C> class A { vector<B<C>> container; ... Iterator InsertItem(C item) {...} } What I want is a way to modify the existing vector iterator to return an Iterator which will retu...

What is the normal convention for naming and using iterators in C++?

I feel like I'm unprofessional in the way I name and use iterators. What I mean by that is that I "feel" like I should be calling them something else, but I always name them based on the "it_" prefix, and after a while, in a long function, the names start to all look alike. Additionally, I always wonder if I'm doing things a "strange" w...

How do I use a recursive array iterator to process a multidimensional array?

I'm trying to get something like this working: function posts_formatter (&$posts){ foreach ($posts as $k => $v){ if (is_array($v)){ posts_formatter($v); }else{ switch (strtolower($k)){ # make email addresses lowercase case (strpos($k, 'email') !== FALSE): ...

What happens if you call the same iterator twice on the same collection?

If I set up an iterator for myList: Iterator iter = myList.iterator(); while(iter.hasNext()) { MyObj myObj = (MyObj)iter.next(); doPrint(myObj.toString()); } And I call it a second time: while(iter.hasNext()) { MyObj myObj = (MyObj)iter.next(); doPrint(myObj.toString()); } Will it go back to the start of the collect...

Java: why are iterators not copyable

I would think that Iterator.copy() would be quite a handy function. You could implement iterator filters in a much better way. For example, the only reason in Googles Java Collection for the filter (and similar) functions to use UnmodifiableIterator (which is just an Iterator without remove) is because you cannot implement such a filter...

Java: why does Collection.addAll can not accept Iterables?

Hi, I wonder why the Collection.addAll() method only accepts other Collections but not Iterables. Why is that? Any similar method to do that for Iterables? ...

itertools or hand-written generator - what is preferable?

I have a number of Python generators, which I want to combine into a new generator. I can easily do this by a hand-written generator using a bunch of yield statements. On the other hand, the itertools module is made for things like this and to me it seems as if the pythonic way to create the generator I need is to plug together various ...

How to make sure iterators do not overpass end() ?

Hi! I have been using advance on some iterators, but I am afraid of a possible leapfrog above end(). I would like to make sure my iterators stay between the bounds, I thought of the distance but it seems it does not return what I would be expecting (non-positive values when iterators overpass end()). How would you make sure there is no ...

understanding zip function

All discussion is about python 3.1.2; see Python docs for the source of my question. I know what zip does; I just don't understand why it can be implemented like this: def zip(*iterables): # zip('ABCD', 'xy') --> Ax By iterables = map(iter, iterables) while iterables: yield tuple(map(next, iterables)) Let's say I ...

STL iterator into constructor

Id' like to know how to write a constructor for a custom class (a linked list in this case) that accepts any STL input iterator. I have already created a custom Iterator class which is tied to my List class. This works fine. template <typename T> List<T>::List(Iterator beg, Iterator end) : first_(0) { while (beg != end) ...

ClassCastException in Java foreach loop

In what circumstances can ClassCastException occur in the code below: import java.util.Arrays; import java.util.List; public class Generics { static List getObjects() { return Arrays.asList(1, 2, 3); } public static void main(String[] args) { List<String> list = getObjects(); for (Object o : list) ...

Converting types in Java

Hi fellas, Java noob here, I have an Iterator<TypeA> that I want to convert to Iterator<TypeB>. TypeA and TypeB cannot be directly cast to each other but I can write a rule how to cast them. How can I accomplish this? Should I extend and override Iterator<TypeA>'s next, hasNext and remove methods? Thanks. ...

Erasing an element from the vector during iteration c++

Hi, I wrote this method to find the minor of a sparse matrix: SpMatrixVec SparseMatrix::minor(SpMatrixVec matrix, int col) const{ SpMatrixVec::iterator it = matrix.begin(); int currRow = it->getRow(); int currCol = col; while(it != matrix.end()) { if(it->getRow() == currRow || it->getCol() == currCol){ ...

iterator to pointer or reference - ERROR

Hi, I have this: //function definition //Point and Range are classes made of 2 ints Point barycenter_of_vector_in_range(vector<cv::Point> &points, cv::Range range); //In other place... vector<vector<Point> > tracks_; //it has some content for (vector< vector<Point> >::const_iterator track = tracks_.begin(); track != tracks_.end(); tr...

Java Tree Iterator Help

I am trying to write a class that implements a tree using an array and I need some help to write an Iterator method that which returns an iterator of the elements stored in the tree. Here is my code. import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ArrayTree<E> implements Tree<E>{ protected ...

Returning an iterator in a multi threaded environment, a good idea?

Is it a good idea to return an iterator on a list in an object that is used and shared in a multi threaded environment? class RequestList { public: RequestList::RequestList(); RequestList::~RequestList(); std::list<boost::shared_ptr<Request> >::iterator GetIterator(); int ListSize(); void AddItem(boost::shared_ptr<R...

Iterator for array

How to get an iterator for an array in java? int[] arr={1,2,3}; for(int i:arr) System.out.println(i); How does the above for-each loop work ? Is the array converted to a list to get the iterator ? ...

Iterator for vector of pointers not dereferencing correctly

Here is my issue: I have a std::vector<AguiWidgetBase*> which is used to keep track of child controls. I have these two functions to return iterators: std::vector<AguiWidgetBase*>::const_iterator AguiWidgetBase::getChildBeginIterator() const { return children.begin(); } std::vector<AguiWidgetBase*>::const_iterator AguiWidgetBase:...