Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As far as I know, there is no such class in C++11.</p> <p>Anyway, I tried to improve your implementation. I made it <em>non-template</em>, as I don't see any <em>advantage</em> in making it <em>template</em>. On the contrary, it has one major disadvantage : that you cannot create the range at runtime, as you need to know the template arguments at compile time itself.</p> <pre><code>//your version auto x = range&lt;m,n&gt;(); //m and n must be known at compile time //my version auto x = range(m,n); //m and n may be known at runtime as well! </code></pre> <p>Here is the code:</p> <pre><code>class range { public: class iterator { friend class range; public: long int operator *() const { return i_; } const iterator &amp;operator ++() { ++i_; return *this; } iterator operator ++(int) { iterator copy(*this); ++i_; return copy; } bool operator ==(const iterator &amp;other) const { return i_ == other.i_; } bool operator !=(const iterator &amp;other) const { return i_ != other.i_; } protected: iterator(long int start) : i_ (start) { } private: unsigned long i_; }; iterator begin() const { return begin_; } iterator end() const { return end_; } range(long int begin, long int end) : begin_(begin), end_(end) {} private: iterator begin_; iterator end_; }; </code></pre> <p>Test code:</p> <pre><code>int main() { int m, n; std::istringstream in("10 20"); if ( in &gt;&gt; m &gt;&gt; n ) //using in, because std::cin cannot be used at coliru. { if ( m &gt; n ) std::swap(m,n); for (auto i : range(m,n)) { std::cout &lt;&lt; i &lt;&lt; " "; } } else std::cout &lt;&lt;"invalid input"; } </code></pre> <p>Output:</p> <p>10 11 12 13 14 15 16 17 18 19</p> <p><a href="http://coliru.stacked-crooked.com/a/2cacfb322d263500" rel="noreferrer">Onine demo</a>.</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. 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.
 

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