Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Boost has <a href="http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/counting_iterator.html" rel="noreferrer">counting_iterator</a> as far as I know, which seems to allow only incrementing in steps of 1. For full xrange functionality you might need to implement a similar iterator yourself.</p> <p>All in all it could look like this (edit: added an iterator for the third overload of xrange, to play around with boost's iterator facade):</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/iterator/counting_iterator.hpp&gt; #include &lt;boost/range/iterator_range.hpp&gt; #include &lt;boost/foreach.hpp&gt; #include &lt;boost/iterator/iterator_facade.hpp&gt; #include &lt;cassert&gt; template &lt;class T&gt; boost::iterator_range&lt;boost::counting_iterator&lt;T&gt; &gt; xrange(T to) { //these assertions are somewhat problematic: //might produce warnings, if T is unsigned assert(T() &lt;= to); return boost::make_iterator_range(boost::counting_iterator&lt;T&gt;(0), boost::counting_iterator&lt;T&gt;(to)); } template &lt;class T&gt; boost::iterator_range&lt;boost::counting_iterator&lt;T&gt; &gt; xrange(T from, T to) { assert(from &lt;= to); return boost::make_iterator_range(boost::counting_iterator&lt;T&gt;(from), boost::counting_iterator&lt;T&gt;(to)); } //iterator that can do increments in steps (positive and negative) template &lt;class T&gt; class xrange_iterator: public boost::iterator_facade&lt;xrange_iterator&lt;T&gt;, const T, std::forward_iterator_tag&gt; { T value, incr; public: xrange_iterator(T value, T incr = T()): value(value), incr(incr) {} private: friend class boost::iterator_core_access; void increment() { value += incr; } bool equal(const xrange_iterator&amp; other) const { //this is probably somewhat problematic, assuming that the "end iterator" //is always the right-hand value? return (incr &gt;= 0 &amp;&amp; value &gt;= other.value) || (incr &lt; 0 &amp;&amp; value &lt;= other.value); } const T&amp; dereference() const { return value; } }; template &lt;class T&gt; boost::iterator_range&lt;xrange_iterator&lt;T&gt; &gt; xrange(T from, T to, T increment) { assert((increment &gt;= T() &amp;&amp; from &lt;= to) || (increment &lt; T() &amp;&amp; from &gt;= to)); return boost::make_iterator_range(xrange_iterator&lt;T&gt;(from, increment), xrange_iterator&lt;T&gt;(to)); } int main() { BOOST_FOREACH(int i, xrange(10)) { std::cout &lt;&lt; i &lt;&lt; ' '; } BOOST_FOREACH(int i, xrange(10, 20)) { std::cout &lt;&lt; i &lt;&lt; ' '; } std::cout &lt;&lt; '\n'; BOOST_FOREACH(int i, xrange(0, 46, 5)) { std::cout &lt;&lt; i &lt;&lt; ' '; } BOOST_FOREACH(int i, xrange(10, 0, -1)) { std::cout &lt;&lt; i &lt;&lt; ' '; } } </code></pre> <p>As others are saying, I don't see this buying you much over a normal for loop.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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