Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ std::iterator without using std::vector or std::list internally
    text
    copied!<p>I'm trying (as an exercise) to create a simple numeric range class in C++. It will let you iterate through evenly spaced doubles (like the numpy/Python <code>arange</code>):</p> <p><strong>What I'd like to do (but with an iterator):</strong></p> <pre><code>double lower = ..., upper = ..., delta = ...; for (double val = lower; val &lt; upper; val += delta) { // do something with val f(val); } // include the last val to guarantee upper is included or exceeded f(val); // do something with val </code></pre> <p><strong>Desired equivalent iterator code:</strong> </p> <pre><code>double lower = ..., upper = ..., delta = ...; NumericRange nr(lower, upper, delta); for (NumericRange::const_iterator iter = nr.begin(); iter != nr.end(); iter++) { f(*iter); } </code></pre> <p>I'd like my iterator to be compatible with STL iterators so I can reuse code (iterating through a NumericRange should be equivalent to iterating through std::vector).</p> <p>I've had success simply storing the values in a std::vector (and then using the std::vector's iterator). This is how everything I've found online has solved this problem. However, it really isn't necessary to store the entire list.</p> <p><strong>Is there a way to avoid storing the entire set of values? Is there some <code>iterable</code> class I can inherit from and override <code>++</code>, <code>==</code>, etc. to get the desired effect without storing the <code>std::vector&lt;double&gt;</code>?</strong> <br></p> <p>(I'd really like to know how to do this without BOOST, even though it's great. I'm asking this because I'd like to learn how to <em>write</em> (from scratch) something like a BOOST solution. I definitely know that part of software engineering is using the tools created by others, but I really want to learn how those tools are designed and built.)</p> <p><strong>My iterable NumericRange class (using <code>std::vector&lt;double&gt;</code> internally):</strong></p> <pre><code>class NumericRange { protected: double lower, upper, delta; std::vector&lt;double&gt; sorted_range; public: typedef std::vector&lt;double&gt;::const_iterator const_iterator; NumericRange() { lower = upper = delta = std::numeric_limits&lt;double&gt;::quiet_NaN(); // vector is constructed empty } NumericRange(double lower_param, double upper_param, double delta_param) { lower = lower_param; upper = upper_param; delta = delta_param; assert(upper_param &gt; lower_param); double val; // note: can be much faster without push_back for (val = lower_param; val &lt; upper_param; val += delta_param) { sorted_range.push_back(val); } // ensure the upper_value is contained or surpassed sorted_range.push_back(val); } // to prevent comparison of the entire vector bool operator ==(const NumericRange &amp; rhs) const { return lower == rhs.lower &amp;&amp; upper == rhs.upper &amp;&amp; delta == rhs.delta; } // note: this class doesn't really need to store the values in a // vector, but it makes the iterator interface much easier. const_iterator begin() const { return sorted_range.begin(); } const_iterator end() const { return sorted_range.end(); } double get_lower() const { return lower; } double get_upper() const { return upper; } double get_delta() const { return delta; } size_t size() const { return sorted_range.size(); } void print() const { std::cout &lt;&lt; "[ " &lt;&lt; lower &lt;&lt; " : " &lt;&lt; upper &lt;&lt; ": +=" &lt;&lt; delta &lt;&lt; " ]" &lt;&lt; std::endl; } }; </code></pre>
 

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