You can have a following simple equivalent to Java Iterable
interface:
template <typename T, typename U>
struct iterable {
T _begin;
U _end;
iterable(T begin, U end)
: _begin(begin),
_end(end)
{}
T begin() {
return _begin;
}
U end() {
return _end;
}
};
If you wonder why there are T
and U
when the begin iterator and end iterator should be the same. The reason is that some containers don't have those two iterators of the same type.
Furthermore, you can implement a helper function make_iterable
like:
template <typename T, typename U>
iterable<T, U> make_iterable(T t, U u) {
return iterable<T,U>(t, u);
}