iterator

Java: How to Implement Iterable

Given the following code, how can I iterate over an object of type ProfileCollection? public class ProfileCollection implements Iterable { private ArrayList<Profile> m_Profiles; public Iterator<Profile> iterator() { Iterator<Profile> iprof = m_Profiles.iterator(); return iprof; } ... p...

How can I form an ordered list of values extracted from HashMap?

My problem is actually more nuanced than the question suggests, but wanted to keep the header brief. I have a HashMap<String, File> of File objects as values. The keys are String name fields which are part of the File instances. I need to iterate over the values in the HashMap and return them as a single String. This is what I have cu...

Infinite yields from an iterator.

I'm trying to learn some ruby. Imagine I'm looping and doing a long running process, and in this process I want to get a spinner for as long as necessary. So I could do: a=['|','/','-','\\'] aNow=0 # ... skip setup a big loop print a[aNow] aNow += 1 aNow = 0 if aNow == a.length # ... do next step of process print "\b" But I thought i...

Implementing empty iterators

I have this code: public IEnumerable<int> Iterator { get { if (false) yield return -1; } } It is fairly ugly, but when you try to refactor it to: public IEnumerable<int> Iterator { get { return null; } } The following code breaks: foreach (var item in obj.Iterator) { } How would you go about cl...

Performance Advantages to Iterators?

What (if any) performance advantages are offered by using iterators. It seems like the 'Right Way' to solve many problems, but does it create faster/more memory-conscious code? I'm thinking specifically in Python, but don't restrict answers to just that. ...

What's the cleanest way to walk and unwalk a std::vector using iterators?

I have a situation where I'm marching through a vector, doing things: std::vector::iterator iter = my_list.begin(); for ( ; iter != my_list.end(); ++iter ) { if ( iter->doStuff() ) // returns true if successful, false o/w { // Keep going... } else { for ( ; iter != m_list.begin(); --iter ) // ...This won't work......

Why does my Boost.Regex search report only one match iteration?

I am trying to find out how many regex matches are in a string. I'm using an iterator to iterate the matches, and and integer to record how many there were. long int before = GetTickCount(); string text; boost::regex re("^(\\d{5})\\s(\\d{8})\\s(.*)\\s(.*)\\s(.*)\\s(\\d{8})\\s(.{1})$"); char * buffer; long length; long count; ifstream ...

Returning a pointer to a vector element in c++.

I have a vector of myObjects in global scope. I have a method which uses a std::vector<myObject>::const_iterator to traverse the vector, and doing some comparisons to find a specific element. Once I have found the required element, I want to be able to return a pointer to it (the vector exists in global scope). If I return &iterator, am...

Iterability in Python

I am trying to understand Iterability in Python. As I understand, __iter__() should return an object that has next() method defined which must return a value or raise StopIteration exception. Thus I wrote this class which satisfies both these conditions. But it doesnt seem to work. What is wrong? class Iterator: def __init__(self)...

XPathNodeIterator over an array of XPathNavigable objects?

Hello, I have an array of objects that are IXpathNavigable. I want to access the array through an xsl extention object, so I should probably do that by using an XPathNodeIterator. But how do I properly create the NodeIterator so that it iterates over the array? ...

Using a pointer to an object stored in a vector... c++

I have a vector of myObjects in global scope. std::vector<myObject> A method is passed a pointer to one of the elements in the vector. Can this method increment the pointer, to get to the next element, myObject* pmObj; ++pmObj; // the next element ?? or should it be passed an std::Vector<myObject>::iterator and increment that ins...

C++ STL Vectors: Get iterator from index?

So, I wrote a bunch of code that accesses elements in an stl vector by index[], but now I need to copy just a chunk of the vector. It looks like vector.insert(pos, first, last) is the function I want... except I only have first and last as ints. Is there any nice way I can get an iterator to these values? ...

How to convert c++ std::list element to multimap iterator

Hello all, I have std::list<multimap<std::string,std::string>::iterator> > Now i have new element: multimap<std::string,std::string>::value_type aNewMmapValue("foo1","test") I want to avoid the need to set temp multimap and do insert to the new element just to get its iterator back so i could to push it back to the: std::list<m...

Py3k memory conservation by returning iterators rather than lists

Many methods that used to return lists in Python 2.x now seem to return iterators in Py3k Are iterators also generator expressions? Lazy evaluation? Thus, with this the memory footprint of python is going to reduce drastically. Isn't it? What about for the programs converted from 2to3 using the builtin script? Does the builtin tool e...

Python: an iteration over a non-empty list with no if-clause comes up empty. Why?

How can an iterator over a non-empty sequence, with no filtering and no aggregation (sum(), etc.), yield nothing? Consider a simple example: sequence = ['a', 'b', 'c'] list((el, ord(el)) for el in sequence) This yields [('a', 97), ('b', 98), ('c', 99)] as expected. Now, just swap the ord(el) out for an expression that takes the firs...

any way to loop iteration with same item in python?

Hello. It's a common programming task to loop iteration while not receiving next item. For example: for sLine in oFile : if ... some logic ... : sLine = oFile.next() ... some more logic ... # at this point i want to continue iteration but without # getting next item from oFile. How can this be done in python? ...

How to get the array index or iteration number with an each iterator?

I'm iterating through an array in ruby with each. Is there an easy way to get the iteration number or array index without going back to a for loop? ...

When will STL iterator be equal to zero?

I have a program which is like this list<int>:: iterator n = alist.begin(); while(n!= (list<int>::iterator)0) { printf("Element is %d\n",*n); n = alist.erase(n); } So here i am comparing iterator with zero. but after deleting the last element the compiler is showing this error. *** glibc detected *** ./new: free(): invalid p...

How to tell the difference between an iterator and an iterable?

In Python the interface of an iterable is a subset of the iterator interface. This has the advantage that in many cases they can be treated in the same way. However, there is an important semantic difference between the two, since for an iterable __iter__ returns a new iterator object and not just self. How can I test that an iterable is...

Distinction between iterator and enumerator

An interview question for a .NET 3.5 job is "What is the difference between an iterator and an enumerator"? This is a core distinction to make, what with LINQ, etc. Anyway, what is the difference? I can't seem to find a solid definition on the net. Make no mistake, I can find the meaning of the two terms but I get slightly different an...