iterator

Python newbie class design question.

I'm trying to figure out the best way to design a couple of classes. I'm pretty new to Python (and OOP in general) and just want to make sure that I'm doing this right. I have two classes: "Users" and "User". class User(object): def __init__(self): pass class Users(object): def __init__(self): self.users = [] ...

Clever Uses of .Net 2 Iterators

C# 2 and VB.Net 8 introduced a new feature called iterators, which were designed to make it easier to return enumerables and enumerators. However, iterators are actually a limited form of coroutines, and can be used to do many useful things that have nothing to do with collections of objects. What non-standard uses of iterators have yo...

Not using iterators into a resized vectors

I read in The C++ Programming Language : Special Edition Don't use iterators into a resized vector Consider this example. vector< int >::iterator it = foo.begin(); while ( it != foo.end() ) { if ( // something ) { foo.push_back( // some num ); } ++it; } Is there a problem with this? After the vector was resized, would the...

gcc reverse_iterator comparison operators missing?

I am having a problem using const reverse iterators on non-const containers with gcc. Well, only certain versions of gcc. #include <vector> #include <iostream> using namespace std; int main() { const char v0[4] = "abc"; vector<char> v(v0, v0 + 3); // This block works fine vector<char>::const_iterator i; for (i = ...

How do you return an Iterator in Scala?

What must I do in order to be able to return an Iterator from a method/class ? How would one add that trait to a class? ...

Cleanest way to get last item from Python iterator

What's the best way of getting the last item from an iterator in Python 2.6? For example, say my_iter = iter(range(5)) What is the shortest-code / cleanest way of getting 4 from my_iter? I could do this, but it doesn't seem very efficient: [x for x in my_iter][-1] ...

java: resetting ListIterator?

I need to traverse a LinkedList a number of times, in a way that suggests using ListIterator. Is there a way to reset a ListIterator? or is it better to just create a new one? (and what if I can't because I don't have access to the list?) edit: and is there a way to create a ListIterator that points to the end of the list? (so that has...

How do you avoid code duplication when implementing const and non-const iterators?

I'm implementing a custom container with an STL-like interface. I have to provide a regular iterator and a const iterator. Most of the code for the two versions of the iterators is identical . How can I avoid this duplication? For example, my container class is Foo, and I'm implementating FooIterator and FooConstIterator. Both of th...

Iterating the std::list through a const_iterator

is it possible to iterate through until the end of list in main() function using the const_iterator? I tried using iter->end() but i can't figure it out. #include <list> #include <string> using std::list; using std::string; class list_return { public: list <string>::const_iterator get_list() { _list.push_back("1"); _list.push_back("2")...

Python looping: idiomatically comparing successive items in a list

I need to loop over a list of objects, comparing them like this: 0 vs. 1, 1 vs. 2, 2 vs. 3, etc. (I'm using pysvn to extract a list of diffs.) I wound up just looping over an index, but I keep wondering if there's some way to do it which is more closely idiomatic. It's Python; shouldn't I be using iterators in some clever way? Simply loo...

Best way to get the index of an iterator?

I'm iterating over a vector and need the index the iterator is currently pointing at. AFAIK this can be done in two ways: it - vec.begin() std::distance(vec.begin(), it) Which one is better or preferred and why? ...

Most of the Iterators and Iterables methods are LAZY! What does this mean.

1 of the presentation says "These methods are LAZY!" Iterable transform(Iterable, Function)* Iterable filter(Iterable, Predicate)* T find(Iterable<T>, Predicate) Iterable concat(Iterable<Iterable>) Iterable cycle(Iterable) T getOnlyElement(Iterable<T>) Iterable<T> reverse(List<T>) Can someone help me understand what they mean by this,...

Can we use for-each loop for iterating the objects of Iterator type?

If we do the following we get error: class FGH{ public static Iterator reverse(List list) { Collections.reverse(list); return list.iterator(); } public static void main(String[] args) { List list = new ArrayList(); list.add("1"); list.add("2"); list.add("3"); /*for(Iterator it:reverse(list)) Iter...

Iterating over the types in a boost::variant

I'm using a boost variant to hold some generated types, right now my code generator creates a header with the types and a variant capable of holding them. At initialization time, I'd like to iterate over the allowable types in the variant, not the types the variant is holding at the moment. Can I do this with a variant? ...

How can I determine the type of object an ArrayList iterator will return next?

I have an ArrayList and I am using an iterator to run through it. I need to find out what type of object is next: Iterator vehicleIterator = vehicleArrayList.iterator(); while(vehicleIterator.hasNext()) { //How do I find the type of object in the arraylist at this point // for example, is it a car, bus etc... } Thanks ...

How are Iterators implemented in Java?

Does an instance of an Iterator opened on a collection keep the whole collection in memory and access a position that increments every time next() is called? Or am I missing something? ...

Compiler error with boost iterator adaptor

I am trying to write a simple STL iterator for CArray MFC class using boost iterator adaptor. This is my code: #include <boost/iterator/iterator_adaptor.hpp> #include <afxtempl.h> class CArrIter : public boost::iterator_adaptor< CArrIter , int, int, boost::random_access_traversal_tag > { public: CArrIter(CArray<int,in...

Java Iterator implementation - next() and hasNext() enforcing order

I have an implementation of java.util.Iterator which requires that the call to next() should always be proceeded by a call to hasNext(). (This is because results are returned asynchronosly in a multi threaded environment and it is never clear how many more results there might be). Would it be 'correct' to properly document this in the J...

Python: Why can't I iterate over a list? Is my exception class borked?

I've already looked at this question: http://stackoverflow.com/questions/1152238/python-iterators-how-to-dynamically-assign-self-next-within-a-new-style-class but this doesn't help me because I want to iterate of an attribute of the error which is a list (ie, already iterable) without having to use the attribute explicitly. I'm looking ...

Iterator for custom container with derived classes

I've a custom container which is implemented in two different ways, but with a single interface. some thing like this. class Vector { virtual Iterator begin() = 0; virtual Iterator end () = 0 ; ... // some more functions. } ; class VectorImplA : public Vector { Iterator begin() { return m_...