iterator

Can someone post an example of creating a boost inv_adjacency_iterator using inv_adjacency_iterator_generator?

Given definitions: typedef typename boost::graph_traits::adjacency_iterator adjacency_iter; typedef typename boost::inv_adjacency_iterator_generator::type inv_adjacency_iter; I am interested in semantics of boost::tie(i, end) = inv_adjacent_vertices((*start); adjacent_vertices works fine where inv_adjacent_vertices fails with the foll...

How do I escape the const_iterator trap when passing a const container reference as a parameter

I generally prefer constness, but recently came across a conundrum with const iterators that shakes my const attitude annoys me about them: MyList::const_iterator find( const MyList & list, int identifier ) { // do some stuff to find identifier return retConstItor; // has to be const_iterator because list is const } The idea t...

C++ LINQ-like iterator operations

Having been tainted by Linq, I'm reluctant to give it up. However, for some things I just need to use C++. The real strength of linq as a linq-consumer (i.e. to me) lies not in expression trees (which are complex to manipulate), but the ease with which I can mix and match various functions. Do the equivalents of .Where, .Select and ....

escaping the .each { } iteration early in Ruby

code: c = 0 items.each { |i| puts i.to_s # if c > 9 escape the each iteration early - and do not repeat c++ } I want to grab the first 10 items then leave the "each" loop. What do I replace the commented line with? is there a better approach? something more Ruby idiomatic? ...

C++: iterating over a list of a generic type

Yet again I find myself struggling with the C++ syntax. I'm trying to iterate over a list of generic objects. That is I have objects of a class Event<Q>, crammed into a std::list<Event<Q> >. So I'm trying to get an iterator over the list and intuitively thought that std::list<Event<Q> >::iterator it; for (it = events.begin(); it != ev...

Best way to code iterator in javascript

I'm building a complex object in Javascript and I want to expose an iterator over an internal collection of the object. The only way I can think of is the usual way of exposing iterators in prototype.js : customObject.each(function (item) { ... }) with the provided function being called by the iterator each for every item in the col...

Java: Pass by reference / ListIterator.add()

Java doesn't pass variables by reference. In that case, how do data structures like ListIterator make changes to their corresponding list? Here is an example iterator I am writing: public class OdpIterator<E> implements ListIterator<E> { private OdpList<E> list; private int cursor; public OdpIterator(OdpList<E> list) { ...

what is the c# equivalent of Iterator in Java

I am manually converting Java to C# and have the following code: for (Iterator<SGroup> theSGroupIterator = SGroup.getSGroupIterator(); theSGroupIterator .hasNext();) { SGroup nextSGroup = theSGroupIterator.next(); } Is there an equivalent of Iterator in C# or is there a better C# idiom? ...

C++ Template + Iterator (noob question)

My disclaimer here is that I started teaching myself C++ about a week ago and my former experience with programming has been with dynamic languages (Python, javascript). I'm trying to iterate though the contents of a vector using a generic function to print out the items: #include <iostream> #include <algorithm> #include <vector> usin...

Make my C++ Class iterable via BOOST_FOREACH

I have a class which I want to expose a list of structs (which just contain some integers). I don't want the outside to modify these data, just iterate over it and read them Example: struct TestData { int x; int y; // other data as well } class IterableTest { public: // expose TestData here }; now in my code I want to use...

iterate vector, remove certain items as I go.

I have a std::vector m_vPaths; I will iterate this vector and call ::DeleteFile(strPath) as I go. If I successfully delete the file, I will remove it from the vector. My question is can I get around having to use two vectors? Is there different data structure that might be better suited for what I need to do? example: using iterator...

confused __iter__

i am a reading the chapter on operator over flow and am very confused how __iter__ works. I have gone to several websites to find different examples but still dont quite understand how it works. I am wondering if someone has a really simple approach to on how to understand this function. Main problem on def__iter__(self): return self ...

C# Itreator best approach

Apart from (IEnumerable Returns GetEnumerator() ,for "foreach" IEnumerble is essential) almost the following two approaches allow us to iterate over the collection.What is the advantage of one over another ? (I am not asking the difference between IEnumerable and IEnumerator). static void Main() { IEnumerator<int> em = MyEnumerato...

[Ruby] Declaring instance variables iterating over a hash!!

Hi, i want to do the following: I want to declare the instance variables of a class iterating over a dictionary. Let's assume that i have this hash hash = {"key1" => "value1","key2" => "value2","key3" => "value3"} and i want to have each key as instance variable of a class. I want to know if i could declare the variables iterating o...

Iterators at critical logic

In a recent interview one peculiar question has been asked a[]= { 1,2,3,4,5,6,7,8,9,10} When an array is given with specified starting index i have to iterate it till i traverse all elements. I mean suppose the starting index is "5" i have to start from 6,7,8,9,10,5,4,3,2,1.Please carefully look at the sequence ,how can one create ...

Limit a ListIterator to the first N elements (optimized)

What is a simple and fast way to get an iterator that returns at most N elements from the start of a List? The simplest versions I could come up with are: #1: import com.google.common.collect.Iterators; // ... public static <E> Iterator<E> lengthLimitedIterator(Iterable<E> source, int maxLen) { return Iterators.partition(source....

Does changing vector size spoil iterators?

I found that this C++ code: vector<int> a; a.push_back(1); a.push_back(2); vector<int>::iterator it = a.begin(); a.push_back(4); cout << *it; print some big random number; but if you add a.push_back(3) between 3rd and 4th lines, it will print 1. Can you explain it to me? ...

How to write a class capable of foreach

It's been a while since Visual Studio added support for a foreach extension that works like vector<int> v(3) for each (int i in v) { printf("%d\n",i); } I want to know how to make any class able to use foreach. Do I need to implement some interface? ...

ClassCastException When Casting HashMap Iterator to a Concrete Type

I am very new to processing.org and Java. I am trying to store objects in a HashMap, and then iterate over the values of the HashMap, calling methods on the stored objects. In order to do that, I assume I need to downcast the iterator to the type of my class, but this throws a ClassCastException ("java.util.HashMap$ValueIterator cannot b...

How to create operator-> in iterator without a container?

template <class Enum> class EnumIterator { public: const Enum* operator-> () const { return &(Enum::OfInt(i)); // warning: taking address of temporary } const Enum operator* () const { return Enum::OfInt(i); // There is no problem with this one! } private: int i; }; I get this warning above. Currently I'm us...