Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ range/xrange equivalent in STL or boost?
    text
    copied!<p>Is there C++ equivalent for python Xrange generator in either STL or boost?</p> <p>xrange basically generates incremented number with each call to ++ operator. the constructor is like this:</p> <pre><code>xrange(first, last, increment) </code></pre> <p>was hoping to do something like this using boost for each:</p> <pre><code>foreach(int i, xrange(N)) </code></pre> <p>I. am aware of the for loop. in my opinion they are too much boilerplate.</p> <p>Thanks</p> <h2>my reasons:</h2> <p>my main reason for wanting to do so is because i use speech to text software, and programming loop usual way is difficult, even if using code completion. It is much more efficient to have pronounceable constructs.</p> <p>many loops start with zero and increment by one, which is default for range. I find python construct more intuitive</p> <pre><code> for(int i = 0; i &lt; N; ++i) foreach(int i, range(N)) </code></pre> <p>functions which need to take range as argument:</p> <pre><code> Function(int start, int and, int inc); function(xrange r); </code></pre> <p>I understand differences between languages, however if a particular construct in python is very useful for me and can be implemented efficiently in C++, I do not see a reason not to use it. For each construct is foreign to C++ as well however people use it.</p> <p>I put my implementation at the bottom of the page as well the example usage.</p> <p>in my domain i work with multidimensional arrays, often rank 4 tensor. so I would often end up with 4 nested loops with different ranges/increments to compute normalization, indexes, etc. those are not necessarily performance loops, and I am more concerned with correctness readability and ability to modify.</p> <p>for example</p> <pre><code>int function(int ifirst, int ilast, int jfirst, int jlast, ...); versus int function(range irange, range jrange, ...); </code></pre> <p>In the above, if different strids are needed, you have to pass more variables, modify loops, etc. eventually you end up with a mass of integers/nearly identical loops.</p> <p>foreach and range solve my problem exactly. familiarity to average C++ programmer is not high on my list of concerns - problem domain is a rather obscure, there is a lot of meta-programming, SSE intrinsic, generated code.</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