Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This should provide some ideas/solutions: 2 renditions, the second is a little lighter in weight. Both tested using a subrange of a vector and a list ...</p> <pre><code>#include &lt;vector&gt; template &lt;typename T, typename Container = std::vector&lt;T&gt;, typename Iterator = Container::iterator&gt; class RingIterator : public std::iterator &lt;std::bidirectional_iterator_tag, T, ptrdiff_t&gt; { Container&amp; data; Iterator cursor; Iterator begin; Iterator end; public: RingIterator (Container&amp; v) : data(v), cursor(v.begin()), begin(v.begin()), end(v.end()) {} RingIterator (Container&amp; v, const Iterator&amp; i) : data(v), cursor(i), begin(v.begin()), end(v.end()) {} RingIterator (Container&amp; v, const Iterator&amp; i, const Iterator&amp; j) : data(v), cursor(i), begin(i), end(j) {} RingIterator (Container&amp; v, size_t i) : data(v), cursor(v.begin() + i % v.size()), begin(v.begin()), end(v.end()) {} bool operator == (const RingIterator&amp; x) const { return cursor == x.cursor; } bool operator != (const RingIterator&amp; x) const { return ! (*this == x); } reference operator*() const { return *cursor; } RingIterator&amp; operator++() { ++cursor; if (cursor == end) cursor = begin; return *this; } RingIterator operator++(int) { RingIterator ring = *this; ++*this; return ring; } RingIterator&amp; operator--() { if (cursor == begin) cursor = end; --cursor; return *this; } RingIterator operator--(int) { RingIterator ring = *this; --*this; return ring; } RingIterator insert (const T&amp; x) { return RingIterator (data, data.insert (cursor, x)); } RingIterator erase() { return RingIterator (data, data.erase (cursor)); } }; template &lt;typename T, typename Iterator&gt; class CyclicIterator : public std::iterator &lt;std::bidirectional_iterator_tag, T, ptrdiff_t&gt; { Iterator cursor; Iterator begin; Iterator end; public: CyclicIterator (const Iterator&amp; i, const Iterator&amp; j) : cursor(i), begin(i), end(j) {} bool operator == (const CyclicIterator&amp; x) const { return cursor == x.cursor; } bool operator != (const CyclicIterator&amp; x) const { return ! (*this == x); } reference operator*() const { return *cursor; } CyclicIterator&amp; operator++() { ++cursor; if (cursor == end) cursor = begin; return *this; } CyclicIterator operator++(int) { CyclicIterator ring = *this; ++*this; return ring; } CyclicIterator&amp; operator--() { if (cursor == begin) cursor = end; --cursor; return *this; } CyclicIterator operator--(int) { CyclicIterator ring = *this; --*this; return ring; } }; #include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;list&gt; enum { CycleSize = 9, ContainerSize }; template &lt;typename cyclicIterator&gt; void test (cyclicIterator&amp; iterator, size_t mn) { int m = mn; while (m--) for (int n = mn; n--; ++iterator) std::cout &lt;&lt; std::setw(3) &lt;&lt; *iterator &lt;&lt; ' '; --iterator; m = mn; while (m--) for (int n = mn; n--; --iterator) std::cout &lt;&lt; std::setw(3) &lt;&lt; *iterator &lt;&lt; ' '; } template &lt;typename containers&gt; void load (containers&amp; container) { while (container.size() &lt; ContainerSize) container.push_back (container.size()); } void main (void) { typedef std::vector&lt;int&gt; vContainer; typedef vContainer::iterator vIterator; typedef std::list&lt;int&gt; lContainer; typedef lContainer::iterator lIterator; vContainer v; load (v); vIterator vbegin = v.begin() + 1; RingIterator &lt;int, vContainer, vIterator&gt; vring (v, vbegin, v.end()); CyclicIterator &lt;int, vIterator&gt; vcycle (vbegin, v.end()); lContainer l; load (l); lIterator lbegin = l.begin(); ++lbegin; RingIterator &lt;int, lContainer, lIterator&gt; lring (l, lbegin, l.end()); CyclicIterator &lt;int, lIterator&gt; lcycle (lbegin, l.end()); test (vring, CycleSize); test (vcycle, CycleSize); test (lring, CycleSize); test (lcycle, CycleSize); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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