Re:Writing a custom iterator in modern C++
by Olipro on Monday December 21, 2020 @05:47AM
(#25493740)
An iterator is a class. It can contain any private data you desire and mutate in any wonderful and/or stupid ways you desire.
How it decides to compare equal to the .end() instance is also your call. Your example is not a compliant iterator (as in, wouldn't work in a range-based for loop) - yet the same behaviour would be achievable through your own operator++() and operator bool(),
Re:Writing a custom iterator in modern C++
by fjfaase on Monday December 21, 2020 @06:33AM
(#25493951)
I agree, but I feel it is a little sad that such a powerful programming concept as an iterator is hard to implement in C++ if you want to make full use of it. Other program languages have made better choices with respect to this and allow you to write cleaner, shorter code.
The most common case, are forward output iterators, for which I usually define a class that defines the methods more and next, that can be used as:
for (MyIterator it(data); it.more(); it.next())