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

NutCracker
  • 11,485
  • 4
  • 44
  • 68
WAifT39
  • 75
  • 3
  • This question was answered many times , see for example : https://stackoverflow.com/questions/46431762/how-to-implement-standard-iterators-in-class – Mif Jun 02 '20 at 07:51

2 Answers2

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.

Sorin
  • 11,863
  • 22
  • 26
  • 1
    Implementing begin() and end() member functions is the way to go. std::begin() and std::end() rely on them, see: https://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 May 03 '20 at 07:57
  • @besc It's allowed, although not recommended. – cigien May 03 '20 at 14:57
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);
}
NutCracker
  • 11,485
  • 4
  • 44
  • 68