iterator

Need to add an element at the start of an iterator in python

I have a program as follows: a=reader.next() if *some condition holds*: #Do some processing and continue the iteration else: #Append the variable a back to the iterator #That is nullify the operation *a=reader.next()* How do I add an element to the start of the iterator? (Or is there an easier way to do this?) EDIT: OK l...

.NET: is there a "HasNext" method for an IEnumerator ?

With Java Iterators, I have used the hasNext method to determine whether an iteration has more elements (without consuming an element) -- thus, hasNext is like a "Peek" method. My question: is there anything like a "hasNext" or "Peek" method with C#'s generic IEnumerators? ...

Difference between Iterator and Array in c#

Duplicate of What is the purpose/advantage of using yield return iterators in C#? Okay it just not this. Can you explain me in details that how does the two different? Like Returning an Array (Lets say string) or doing a yield return with iterator(also string) I find them to be the same. Please go into detail. ...

Custom Java Iterator with type confusion

I have a generic class which bundles an Object and an order: public class OrderedObject<T> { private int order; private T object; public OrderedObject(int order, T object) { this.order = order; this.object = object; } public int getOrder() { return order; } public T getObject() { ...

Why is this invalid Java? Type of ternary operator output.

Check out this code. // Print object and recurse if iterable private static void deep_print(Object o) { System.out.println(o.getClass().toString() + ", " + o.toString()); boolean iter = false; Iterable<?> i1 = null; Object[] i2 = null; if (o instanceof Iterable<?>) { iter = true; i1 = (Iterable<?>) o; } else if (o ...

Creating an endless Iterator with a given distribution

Given a java.util.Collection what is the easiest way to create an endless java.util.Iterator which returns those elements such that they show up according to a given distribution (org.apache.commons.math.distribution)? ...

How to determine whether output iterator has been modified

I have a template function that takes the following form: template < class ITER1, class ITER2 > bool example(ITER1 Input1, ITER1 Input2, ITER2 Output) { ITER2 OrigOutput(Output); // ... std::copy(Input1, Input2, Output); return (OrigOutput != Output); } And I'm calling example() like this: std::vector < int > Input...

How can I iterate through a string and also know the index (current position)?

Often when iterating through a string (or any enumerable object), we are not only interested in the current value, but also the position (index). To accomplish this by using string::iterator we have to maintain a separate index: string str ("Test string"); string::iterator it; int index = 0; for ( it = str.begin() ; it < str.end...

Memory usage of iterators in C#

What is the impact on memory usage of using lots of iterators in C#? Let's assume a program that performs thousands of foreach loops -- does each loop allocate a temporary object on the heap via a call to GetEnumerator? Does the CLR perform any kind of optimization (e.g. stack allocation of IEnumerator objects)? Or is this simply not ...

iterating through TWO sparse matrices

I'm using boost sparse matrices holding bool's and trying to write a comparison function for storing them in a map. It is a very simple comparison function. Basically, the idea is to look at the matrix as a binary number (after being flattened into a vector) and sorting based on the value of that number. This can be accomplished in this ...

what's the correct way of writing this code?

typedef boost::shared_ptr<config_value_c> config_value_ptr; typedef std::vector<config_value_ptr> config_value_vec; config_value_vec config; typeof (config.iterator ()) it = config.iterator (); I want to extract an iterator to an array of boost pointers to class config_value_c. I know I can specify the iterator as std::vector<config...

Initializing a vector with stream iterators

I'm trying to initialize a vector using iterators and I'm getting a compiler error basically saying that there's no matching function to call. The code reads from a file with an istream_iterator and ends with an input sentinel. Then I try to initialize the vector with those two iterators. #include "std_lib_facilities.h" #include<itera...

Remove elements as you traverse a list in Python

In Java I can do by using an Iterator and then using the .remove() method of the iterator to remove the last element returned by the iterator, like this: import java.util.*; public class ConcurrentMod { public static void main(String[] args) { List<String> colors = new ArrayList<String>(Arrays.asList("red", "green", "blue",...

use different string in function?

Hello Code Wizards, I am a total NOOB in programming (but this is only my second question on stackoverflow :-) ). By a foreach function I get 5 different string values for $Loncoord, $Latcoord, $gui; this I can see with the print_r in the code written below: "-5.68166666667","+24.6513888889","IMG_3308", But I now want to create 5 di...

How does the PHP IteratorIterator class work?

Try as I might I cannot get my head around what the IteratorIterator class actually does. I understand that classes can implement Traversable so the engine knows it can loop using foreach and I realise that IteratorIterator is supposed to convert anything that is Traversable into an Iterator but I cannot for the life of me understand how...

How can I get data iteratively from another class in C++?

In class A, there is a vector V. V is a private member. In class B, I want to print all items of V. What is the best way to do this? It is very easy to get an iterator of a vector in the same class, but not easy in another class. Thank you for reading. ...

java iterator/iterable subinterface

I have an interface for a variety of classes, all of which should implement Iterator, so I have something like public interface A extends Iterable<A> { ...otherMethods()... } For the concrete classes, however, this means I must use public class B implements A { public Iterator<A> iterator() {...} } when I'd prefer (or at least, thi...

About jQuery .each or other iteration

Hi, How to iterate through table if it has this structure: <table> <tr> <td><input class = "fire"> ... </td> <td><input class = "water"> ... </td> </tr> <tr> <td><input class = "fire"> ... </td> <td><input class = "water"> ... </td> </tr> <tr> <td><input class = "fire"> ... </td> <td><input class = "wa...

Taking iterators two at a time?

I'll often represent and process polylines like so: typedef std::vector< Point_t > Polyline_t; double PolylineLength(const Polyline_t& line) { double len = 0.0; for( size_t i = 0; i < line.size()-1; ++i ) len += (line[i+1]-line[i+0]).length(); return len; } The most straightforward conversion to bidirectional iter...

"Email" reader - odd iterator issue

This program reads "emails" (really just a .txt file structured like an email) and does various things with it (storing data in maps and manipulating it). However, I'm having an unusual issue outputting the "message" of an e-mail based on searching for the subject. First - here is the code (you can probably ignore everything except the...