WAifT39
WAifT39

Reputation: 75

Custom container traversal with range-based for loop

In C++, some STL containers like vector, map, string can be traversed by for loops with colon in it.

for instance:

for(auto c:v)

When I'm writing a custom container, can I make it traversed that way like Java (which only need to implement Iterable)?

Upvotes: 6

Views: 526

Answers (2)

NutCracker
NutCracker

Reputation: 12253

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);
}

Upvotes: 0

Sorin
Sorin

Reputation: 11968

Yes, you need to implement some form of iterator and override std::begin(container) and std::end(container) (might work also if you container has begin and end methods).

Internally the code is equivalent to something like the following (this is just to get the point across, the compiler can write it slightly differently, see here for more details).

auto _end = end(v);
for (auto _it = begin(v); _it != _end; ++_it) {  
    auto c = *_it;
    <the rest of loop code>
}

So if your iterator and overrides work as expected it will work for the for loop as well.

Upvotes: 5

Related Questions