5 ms·
Iterators are a very powerful concept, however, it is very cumbersome and often impossible, to implement some interesting iterator based on the standard iterato
by fjfaase 6y ago
Iterators are a very powerful concept, however, it is very cumbersome and often impossible, to implement some interesting iterator based on the standard iterators. This stems from the idea that any iterator can based on a single pointer and use pointer comparison for indicating the start and the end of the contents.
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())
- Olipro 6y agoAn 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(),
- fjfaase 6y agoI 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.
- saurik 6y agoWhy not just use those two operators instead of more and next?