adbrown85/edo — Issue #15: Add a Range object for coupling iterators
Author: adbrown85
Created: 2011-12-18 05:04:53+00:00
State: open

As powerful and flexible as C++ iterators are, they can start to get a little messy when the object contains multiple collections. In that case, what do you call the methods that return the iterators? To me making separate _begin_ and _end_ methods for each collection is pretty ugly. It would be nice to have something to couple the two iterators together.

Of course, _pair_ is always an option, but since the items in that can be different types it requires a bit more typing. Worse, the fields in _pair_ are named _first_ and _second_, which don't mesh very well with the concept of iterating. Then think about if the iterator is over a map where each entry is also a _pair_.

In short, a separate _Range_ class seems like a good idea. It will need to be a template of course. The general use case will be for an object to _typedef_ the iterator like normal as _thing_iterator_. Then _typedef_ a _Range_ of _thing_iterator_ as _thing_range_. Then the user can request the range, pull out _begin_ and _end_, then do the iteration normally with the iterator.

class Bushel {
public:
typedef list::iterator apple_iterator;
typedef Range apple_range;
typedef vector::iterator orange_iterator;
typedef Range orange_range;
apple_range apples();
orange_range oranges();
};