iterator

Java: The difference between iterators and arraylists

How would you explain to someone who has just started programming in Java, what the difference between ArrayLists and Iterators are? Why I would use an iterator instead of using the get() methods of the Arraylist ...

C#: yield return within a foreach fails - body cannot be an iterator block

Consider this bit of obfuscated code. The intention is to create a new object on the fly via the anonymous constructor and yield return it. The goal is to avoid having to maintain a local collection just to simply return it. public static List<DesktopComputer> BuildComputerAssets() { List<string> idTags = GetComputerIdTag...

LINQ Iterator Exception Handling

var trimmed = myStringArray.Select(s => s.Substring(0, 10)); If one of the strings isn't 10 characters long I'd get an ArgumentOutOfRangeException. In this case its fairly trivial to find out and I know I can do s.Substring(0, Math.Min(10, s.Length)) With more complex object construction errors like this aren't always easy to se...

what is the pattern for modifying a collection in C#

What is the pattern (best practice) for such problem -- modifying elements (values) in collection? Conditions: size of the collection is not changed (no element is deleted or added) modification is in-place In C++ it was easy and nice, I just iterated trough a collection and changed the elements. But in C# iterating (using enumerato...

Iterator blocks and inheritance.

Given a base class with the following interface: public class Base { public virtual IEnumerable<string> GetListOfStuff() { yield return "First"; yield return "Second"; yield return "Third"; } } I want to make a derived class that overrides the method, and adds its own stuff, like so: public class D...

How to provide stl like container with public const iterator and private non-const iterator?

Hello, I have a class that includes a std::list and wish to provide public begin() and end() for const_iterator and private begin() and end() for just plain iterator. However, the compiler is seeing the private version and complaining that it is private instead of using the public const version. I understand that C++ will not overload...

Are Multiple Iterators possible in php?

PLEASE CHECK ANSWERS by VolkerK too, he provided another solution, but I can't mark two posts as answer. :( Good day! I know that C# allows multiple iterators using yield, like described here: http://stackoverflow.com/questions/1754041/is-multiple-iterators-is-possible-in-c In PHP there is and Iterator interface. Is it possible to i...

Python: concatenate generator and item

I have a generator (numbers) and a value (number). I would like to iterate over these as if they were one sequence: i for i in tuple(my_generator) + (my_value,) The problem is, as far as I undestand, this creates 3 tuples only to immediately discard them and also copies items in "my_generator" once. Better approch would be: def con(...

Java - problems iterating through an ArrayList

Ok so I have an ArrayList (arrBok), which is full of book objects (the code is in Norwegian, so pay no attention to that please). I want to make a public method which iterates through all the objects in the arraylist. When I execute the code, it just seems to run in an infinite loop, not producing any return values. Here is the relevan...

Why must I rewind IteratorIterator

$arrayIter = new ArrayIterator( array(1, 2) ); $iterIter = new IteratorIterator($arrayIter); var_dump($iterIter->valid()); //false var_dump($arrayIter->valid()); //true If I first call $iterIter->rewind(), then $iterIter->valid() is true. I'm curious why it requires that rewind() be called. I imagine there's good reason for it, but I ...

Is there a simple way to make lists behave as files (with ftplib)

I'd like to use ftplib to upload program-generated data as lists. The nearest method I can see for doing this is ftp.storlines, but this requires a file object with a readlines() method. Obviously I could create a file, but this seems like overkill as the data isn't persistent. Is there anything that could do this?: session = ftp.new(...

How do I get an Iterator over a vector of objects from a Template?

I'm busy implementing a Graph ADT in C++. I have templates for the Edges and the Vertices. At each Vertex I have a vector containing pointers to the Edges that are incident to it. Now I'm trying to get an iterator over those edges. These are the lines of code: vector<Edge<edgeDecor, vertexDecor, dir>*> edges = this->incidentEdges(); vec...

Python method to remove iterability

Suppose I have a function which can either take an iterable/iterator or a non-iterable as an argument. Iterability is checked with try: iter(arg). Depending whether the input is an iterable or not, the outcome of the method will be different. Not when I want to pass a non-iterable as iterable input, it is easy to do: I’ll just wrap it w...

pushing back an boost::ptr_vector<...>::iterator in another boost::ptr_vector?

Hi all, I have the following code (just typed it in here, might have typos or stuff): typedef boost::ptr_vector<SomeClass> tvec; tvec v; // ... fill v ... tvec vsnap; for(tvec::iterator it = v.begin(); it != v.end(); ++it) { if((*v).anyCondition) vsnap.push_back( it ); // (*it) or &(*it) doesn't work } My problem is now ...

c++ std::map question about iterator order

Hi all, I am a C++ newbie trying to use a map so I can get constant time lookups for the find() method. The problem is that when I use an iterator to go over the elements in the map, elements do not appear in the same order that they were placed in the map. Without maintaining another data structure, is there a way to achieve in order...

syntax help required on templated static member function

I have a bunch of containers of object pointers that I want to iterate through in different contexts to produce diagnostics for them. I'm struggling with the syntax required to define the functions... which, on account of these objects filtering through diverse parts of my application, seem best encapsulated in a dedicated diagnostics cl...

Display `pretty output` of directory content from array

Hi, I'm using the following code to get an array of directories and their sub-directories where each contain file type extension: png. It works great but I need to be able to output the results of the array in a list style format e.g. * Test -> test2.png -> test1.png * subfolder -> test3.png * sub sub folder -> test...

Efficiency of iterators in unordered_map (C++)

I can't seem to find any information on this, so I turn to stackoverflow. How efficient are the iterators of std::tr1::unordered_map in C++? Especially compared to, say, list iterators. Would it make sense to make a wrapper class that also holds all the keys in a list to allow for efficient iteration (my code does use a lot of iterati...

Iterator performance contract (and use on non-collections)

If all that you're doing is a simple one-pass iteration (i.e. only hasNext() and next(), no remove()), are you guaranteed linear time performance and/or amortized constant cost per operation? Is this specified in the Iterator contract anywhere? Are there data structures/Java Collection which cannot be iterated in linear time? java.uti...

How to make a loop over all keys of a HashMap?

I try to do it in the following way: public String getValue(String service, String parameter) { String inputKey = service + ":" + parameter; Set keys = name2value.keySet(); Iterator itr = keys.iterator(); while (itr.hasNext()) { if (inputKey.equal(itr.next())) { return name2value.get(inputKey); ...