iterator

Is there a neater way to get the first occurrence of something?

I have a list which contains a number of things: lista = ['a', 'b', 'foo', 'c', 'd', 'e', 'bar'] I'd like to get the first item in the list that fulfils a predicate, say len(item) > 2. Is there a neater way to do it than itertools' dropwhile and next? first = next(itertools.dropwhile(lambda x: len(x) <= 2, lista)) I did use [item f...

Iterating over key and value of defaultdict dictionaries

The following works as expected: d = [(1,2), (3,4)] for k,v in d: print "%s - %s" % (str(k), str(v)) But this fails: d = collections.defaultdict(int) d[1] = 2 d[3] = 4 for k,v in d: print "%s - %s" % (str(k), str(v)) With: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is no...

Iterator has .next() - is there a way to get the previous element instead of the next one?

I have an Iterator that I use on a HashMap, and I save and load the iterator. is there a way to get the previous key in the HashMap with Iterator? (java.util.Iterator) Update I save it as an attribute in a Red5 connection and then load it back to continue working where i stopped. Another update I'm iterating through the keyset of the...

Making a python iterator go backwards?

Is there anyway to make a python list iterator to go backwards? Basically i have this class IterTest(object): def __init__(self, data): self.data = data self.__iter = None def all(self): self.__iter = iter(self.data) for each in self.__iter: mtd = getattr(self, type(each).__name__) ...

vector iterator not dereferencable at runtime on a vector<vector<vector<A*>*>*>

Hi, I have this destructor that create error at runtime "vector iterator not dereferencable". The gridMatrix is a std::vector<std::vector<std::vector<AtomsCell< Atom<T> * > * > * > * > I added the typename and also the typedef but I still have the error. I will move for this idea of vect of vect* of vect* to use boost::multi_array I ...

private class calling a method from its outer class

Ok, so I have a class for a "Advanced Data Structure" (in this case a kinda tree) SO I implimented a Iterator as a private class with in it. So the iterator needs to implement a remove function to remove the last retuirned element. now my ADT already impliments a remove function, and in this case there is very little (thinking about it...

How to iterate properly across a const set?

I'm working on a program that's supposed to represent a graph. My issue is in my printAdjacencyList function. Basically, I have a Graph ADT that has a member variable "nodes", which is a map of the nodes of that graph. Each Node has a set of Edge* to the edges it is connected to. I'm trying to iterate across each node in the graph and ea...

Is there an easy way to copy an iterator into a list in Java?

I want something like this: public void CopyIteratorIntoList(Iterator<Foo> fooIterator) { List<Foo> fooList = new ArrayList<Foo>(); fooList.addAll(fooIterator); } which should be equivalent to: public void CopyIteratorIntoList(Iterator<Foo> fooIterator) { List<Foo> fooList = new ArrayList<Foo>(); while(fooIterator.has...

Is there a writable iterator in Java?

In C+ one can use iterators for writing to a sequence. Simplest example would be: vector<int> v; for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it) { *it = 42; } I need something more complicated - keep iterator as a class member for a later use. But I don't know how to get this behavior from Java iterators. Are there ...

Iterators for mutable collections in Scala?

I just found out that there are such iterators in Java. Does Scala have iterators with 'set' and 'remove' methods for iterating (and modifying) mutable collections like array? If there is no such iterator then is there a good reason for that? ...

Is there any parallel in Objective-C to C#'s yield keyword

The first time I saw the yield keyword in C# I thought "yuck what a way to junk up the language". Having grown since then and actually used the language I find it so pleasantly simple to express state logic that I'd like to use a similar approach in other development platforms. I'm exploring Objective-C for some support utilities. Is ...

Need help iteratating over an array, retrieve two possibilites, no repeats, for Poker AI

I can't really think of a good way to word this question, nor a good title, and maybe the answer is so ridiculously simple that I am missing it. I am working on a poker AI, and I want to calculate the number of hands that exist which are better than mine. I understand how to that, but what I can't figure out is the best way to iterate ...

Using boost::iterator

I wrote a sparse vector class (see #1, #2.) I would like to provide two kinds of iterators: The first set, the regular iterators, can point any element, whether set or unset. If they are read from, they return either the set value or value_type(), if they are written to, they create the element and return the lvalue reference. Thus, ...

Why only random-access-iterator implements operator+ in C++?

I'd like get far next value for STL list iterator but it doesn't implement operator+, vector has it though. Why and how can I get the value where I want? I think I can do that if I call operator++ several times, but isn't that a little bit dirty? What I want to do is the following: list<int> l; ...omitted... list<int>::iterator itr = ...

Can 'iterator' type just subclass 'const_iterator'?

After another question about iterators I'm having some doubts about custom containers. In my container, iterator is a subclass of const_iterator, so that I get conversion from non-const to const "for free". But is this allowed or are there any drawbacks or non-working scenarios for such a setup? ...

Custom InputIterator for Boost graph (BGL)

Hi, I have a graph with custom properties to the vertices and edges. I now want to create a copy of this graph, but I don't want the vertices to be as complex as in the original. By this I mean that it would suffice that the vertices have the same indices (vertex_index_t) as they do in the original graph. Instead of doing the copying by...

enumerate all combinations in c++

Possible Duplicate: Howto create combinations of several vectors without hardcoding loops in C++? My question is similar to this combinations question but in my case I have N (N > 4) small sets (1-2 items per set for now might go to 3 maybe 4) and want to generate each combination of one item from each set. The current soluti...

Custom iterator which converts values before saving it

A typical forward iterator is expected to implement following methods: value_type& operator*(); value_type* operator->(); I'm writing a custom iterator for a custom container where user expects to see a value_type different from representation of the value inside a container. So when returning a value_type value to user I convert it f...

Deleting elements from STL set while iterating [NEW SOLUTION]

I need to go through a set and remove elements that meet a predefined criteria. This is the test code I wrote: #include <set> #include <algorithm> void printElement(int value) { std::cout << value << " "; } int main() { int initNum[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::set<int> numbers(initNum, initNum + 10); // ...

Generate navigation from a multi-dimensional array

The question: How do I generate navigation, allowing for applying different classes to different sub-items, from a multi-dimensional array? Here is how I was doing it before I had any need for multi-level navigation: Home Pics About and was generated by calling nav(): function nav(){ $links = array( "Home" => "...