Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's actually extremely simple.</p> <p>First of all, take a look at <a href="http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/index.html" rel="nofollow noreferrer">Boost.Iterator</a> library.</p> <p>Second: you need to declare a base class (it's well explained in the example how to proceed) that will be similar to this one.</p> <pre><code>template &lt;class Value&gt; class BaseIterator: boost::iterator_adaptor&lt; ... &gt; {}; </code></pre> <p>You implement the operations to move your pointer around there. Note that because it's an adaptation over an already existing iterator, you can implement it with only a few strokes. It's really impressive.</p> <p>Third, you simply typedef it with the const and non-const versions:</p> <pre><code>typedef BaseIterator&lt;Value&gt; iterator; typedef BaseIterator&lt;const Value&gt; const_iterator; </code></pre> <p>The library explictly show you how to make the <code>const_iterator</code> version be constructible from the <code>iterator</code> version.</p> <p>Fourth, for the reverse thing, there is a special <a href="http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/reverse_iterator.html" rel="nofollow noreferrer">reverse_iterator</a> object, that is built on a regular iterator and move backwards :)</p> <p>All in all, a really elegant and yet fully functional way of defining iterators on custom classes.</p> <p>I regularly write my own container adaptors, and it's less about DRY than simply saving myself some typing!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload