Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>boost::iterator adaptor</code> is the way to go, take my word for it ;)</p> <p>That being said I want to point out a few pitfalls. I don't think I can edit an existing answer, so bear with me.</p> <p>Given that your base iterator is going to be a vector you need to be careful which core interface functions you need to implement. If you want your <code>cycle_iterator</code> to be a random access iterator you need all of the following:</p> <pre><code>increment() decrement() advance(n) distance_to(j) </code></pre> <p>Now <code>distance_to(j)</code> is a somewhat funny concept for a <code>cycle_iterator</code> and its semantics can get you in all kind of trouble. This can be avoided by restricting the iterator category of the adapted iterator to either forward or bidirectional. Like this:</p> <pre><code>template &lt;class BaseIterator&gt; class cycle_iterator : public boost::iterator_adaptor&lt; cycle_iterator // Derived , BaseIterator // Base , boost::use_default // Value , boost::forward_traversal_tag // CategoryOrTraversal &gt; { ... }; </code></pre> <p>In this case you only need to implement increment:</p> <pre><code>void increment() { if (++this-&gt;base_reference() == this-&gt;m_itEnd) { this-&gt;base_reference() = this-&gt;m_itBegin; } } </code></pre> <p>For a bidirectional you also need decrement:</p> <pre><code>void decrement() { if (this-&gt;base_reference() == this-&gt;m_itBegin) { this-&gt;base_reference() = this-&gt;m_itEnd; } --this-&gt;base_reference(); } </code></pre> <p>Disclaimer: I did not run this through a compiler, so I am ready to be embarrassed.</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