6

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)?

1

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Implementing begin() and end() member functions is the way to go. std::begin() and std::end() rely on them, see: en.cppreference.com/w/cpp/iterator/begin I’m not sure whether the standard even allows user-defined specializations of std::begin() and std::end().
@besc It's allowed, although not recommended.
0

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

1 Comment

you can have for (auto const i : arr)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.