114

I found myself writing this just a bit ago:

template <long int T_begin, long int T_end>
class range_class {
 public:
   class iterator {
      friend class range_class;
    public:
      long int operator *() const { return i_; }
      const iterator &operator ++() { ++i_; return *this; }
      iterator operator ++(int) { iterator copy(*this); ++i_; return copy; }

      bool operator ==(const iterator &other) const { return i_ == other.i_; }
      bool operator !=(const iterator &other) const { return i_ != other.i_; }

    protected:
      iterator(long int start) : i_ (start) { }

    private:
      unsigned long i_;
   };

   iterator begin() const { return iterator(T_begin); }
   iterator end() const { return iterator(T_end); }
};

template <long int T_begin, long int T_end>
const range_class<T_begin, T_end>
range()
{
   return range_class<T_begin, T_end>();
}

And this allows me to write things like this:

for (auto i: range<0, 10>()) {
    // stuff with i
}

Now, I know what I wrote is maybe not the best code. And maybe there's a way to make it more flexible and useful. But it seems to me like something like this should've been made part of the standard.

So is it? Was some sort of new library added for iterators over a range of integers, or maybe a generic range of computed scalar values?

12
  • 18
    +1. I would like to have such classes in my utilities. :-) Aug 25, 2011 at 5:16
  • 3
    By the way, what is the point of writing range template function? It doesn't add anything to the usage in which range_class is used. I mean, range<0,10>() and range_class<0,10>() look exactly same! Aug 25, 2011 at 5:18
  • 2
    @Nawaz: Yeah, you're right. I had some odd vision that I could make the function handle differentiating between the dynamic and static case, but I don't think it can be done. Aug 25, 2011 at 5:23
  • 2
    @iammilind: Nawaz asked the same question 35 mins ahead of you ;) Aug 25, 2011 at 6:11
  • 4
    To be pedantic I think this implementation has a bug, which is that you can't use it to iterate over the entire integer range. If you plug in INT_MIN and INT_MAX as your template arguments, INT_MAX when incremented will overflow give INT_MIN and cause infinite loops. "end" in the STL is supposed to be "one past the end" which can't fit inside the integer type itself, so I don't know that this can actually be implemented efficiently for the widest integer type on a given platform. For smaller integer types you can always make it use a wider type internally... Sep 20, 2012 at 2:27

8 Answers 8

69

The C++ standard library does not have one, but Boost.Range has boost::counting_range, which certainly qualifies. You could also use boost::irange, which is a bit more focused in scope.

C++20's range library will allow you to do this via view::iota(start, end).

8
  • 3
    Yes, that's definitely the nature of what I'd be looking for. I'm glad Boost did it. I'm sad the standard committee did not include it for whatever reason. It would've been a great complement to the range-base-for feature. Aug 25, 2011 at 6:18
  • This answer better answers my direct question, and so I will choose it, even though Nawaz's answer is very good. Aug 25, 2011 at 15:11
  • 6
    There has been much progress lately to get ranges into the standard (N4128). See github.com/ericniebler/range-v3 for a proposal and reference implementation.
    – Ela782
    Feb 21, 2015 at 19:27
  • 1
    @Ela782: ... and yet it seems we won't see it in C++17, right?
    – einpoklum
    May 7, 2016 at 18:35
  • 1
    @Andreas Yes, ranges made it into a TS a while ago, but I don't think there is/was ever a reference implementation that made it into the major compilers under the std::experimental::ranges namespace. range-v3 was always sort-of the reference implementation I'd say. But now I believe the basic range stuff has also recently been voted into C++20, so we will indeed get it in std:: soon! :-)
    – Ela782
    Jan 3, 2019 at 11:56
51

As far as I know, there is no such class in C++11.

Anyway, I tried to improve your implementation. I made it non-template, as I don't see any advantage in making it template. 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.

//your version
auto x = range<m,n>(); //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!

Here is the code:

class range {
 public:
   class iterator {
      friend class range;
    public:
      long int operator *() const { return i_; }
      const iterator &operator ++() { ++i_; return *this; }
      iterator operator ++(int) { iterator copy(*this); ++i_; return copy; }

      bool operator ==(const iterator &other) const { return i_ == other.i_; }
      bool operator !=(const iterator &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_;
};

Test code:

int main() {
      int m, n;
      std::istringstream in("10 20");
      if ( in >> m >> n ) //using in, because std::cin cannot be used at coliru.
      {
        if ( m > n ) std::swap(m,n); 
        for (auto i : range(m,n)) 
        {
             std::cout << i << " ";
        }
      }
      else 
        std::cout <<"invalid input";
}

Output:

10 11 12 13 14 15 16 17 18 19

Onine demo.

16
  • 3
    I like it. I thought about a non-template version. And I suppose a good compiler would optimize it well in the case when the values actually are constant. I'll have to test that out. Aug 25, 2011 at 6:00
  • 10
    @Nawaz: I'd still template it, on the integral type :) I'd also propose to alias iterator to const_iterator, have iterator derive from std::iterator and have range implement cbegin and cend. Oh and... why does iterator::operator++ returns a const reference ? Aug 25, 2011 at 6:27
  • 6
    @RedX: Dijkstra has a good write-up on why range labeling is best as [begin, end). @OP: +1 for the pun on range-based loops that isn't a pun :-)
    – Kerrek SB
    Aug 25, 2011 at 11:40
  • 2
    The advantage of the non-template version is that the lengths of your loops do not need to be known at compile time. You could make the integer type templated, of course.
    – CashCow
    Dec 17, 2013 at 15:46
  • 2
    @weeska: That overload is supposed to implement postfix increment v++ which is supposed to return the value before the increment operation took place. I'd advise you to explore the difference between ++i and i++ where i is declared to be int. Jun 17, 2015 at 10:42
13

I wrote a library called range for exactly the same purpose except it is a run-time range, and the idea in my case came from Python. I considered a compile-time version, but in my humble opinion there is no real advantage to gain out the compile-time version. You can find the library on bitbucket, and it is under Boost License: Range. It is a one-header library, compatible with C++03 and works like charm with range-based for loops in C++11 :)

Features:

  • A true random access container with all the bells and whistles!

  • Ranges can be compared lexicographically.

  • Two functions exist(returns bool), and find(returns iterator) to check the existence of a number.

  • The library is unit-tested using CATCH.

  • Examples of basic usage, working with standard containers, working with standard algorithms and working with range based for loops.

Here is a one-minute introduction. Finally, I welcome any suggestion about this tiny library.

4
  • The one-minute introduction says that I don't have access to the Wiki. You need to make your wiki public. Aug 28, 2011 at 20:58
  • @Nicol Bolas I am really sorry, it is public now :) Aug 28, 2011 at 21:13
  • Thank you for this, it's amazing. I feel like more people should know about it. Nov 18, 2019 at 5:06
  • The library no longer exists or at least not exists in the provided link
    – Duloren
    Oct 22, 2022 at 17:23
3

I found that boost::irange was much slower than the canonical integer loop. So I settled on the following much simpler solution using a preprocessor macro:

#define RANGE(a, b) unsigned a=0; a<b; a++

Then you can loop like this:

for(RANGE(i, n)) {
    // code here
}

This range automatically starts from zero. It could be easily extended to start from a given number.

2
  • 9
    Notice that for (RANGE(i, flag? n1: n2)) will yield surprising results, because you failed to follow one of the Basic Rules of Non-Evil Macros, which is to parenthesize all your parameters (including, in this case, b). Your approach also doesn't provide any performance benefit over the non-macro, "range object"–based approach (e.g. Nawaz's answer). Sep 4, 2014 at 23:34
  • I'd personally do #define RANGE(t, v, s, e) (t v = s; v < e; ++v) to be used as for RANGE(unsigned, i, 0, n) { ....
    – Ethouris
    Jun 14, 2021 at 8:56
2

Here is a simpler form which is working nicely for me. Are there any risks in my approach?

r_iterator is a type which behaves, as much as possible, like a long int. Therefore many operators such as == and ++, simply pass through to the long int. I 'expose' the underlying long int via the operator long int and operator long int & conversions.

#include <iostream>
using namespace std;

struct r_iterator {
        long int value;
        r_iterator(long int _v) : value(_v) {}
        operator long int () const { return value; }
        operator long int& ()      { return value; }
        long int operator* () const { return value; }
};
template <long int _begin, long int _end>
struct range {
        static r_iterator begin() {return _begin;}
        static r_iterator end  () {return _end;}
};
int main() {
        for(auto i: range<0,10>()) { cout << i << endl; }
        return 0;
}

(Edit: - we can make the methods of range static instead of const.)

1

This might be a little late but I just saw this question and I've been using this class for a while now :

#include <iostream>
#include <utility>
#include <stdexcept>

template<typename T, bool reverse = false> struct Range final {
    struct Iterator final{
        T value;
        Iterator(const T & v) : value(v) {}
        const Iterator & operator++() { reverse ? --value : ++value; return *this; }
        bool operator!=(const Iterator & o) { return o.value != value; }
        T operator*() const { return value; }
    };
    T begin_, end_;
    Range(const T & b, const T & e)  : begin_(b), end_(e) {
        if(b > e) throw std::out_of_range("begin > end");
    }

    Iterator begin() const { return reverse ? end_ -1 : begin_; }
    Iterator end() const { return reverse ? begin_ - 1: end_; }

    Range() = delete;
    Range(const Range &) = delete;
};

using UIntRange = Range<unsigned, false>;
using RUIntRange = Range<unsigned, true>;

Usage :

int main() {
    std::cout << "Reverse : ";
    for(auto i : RUIntRange(0, 10)) std::cout << i << ' ';
    std::cout << std::endl << "Normal : ";
    for(auto i : UIntRange(0u, 10u)) std::cout << i << ' ';
    std::cout << std::endl;
}
-1

have you tried using

template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function f);

Most of the time fits the bill.

E.g.

template<class T> void printInt(T i) {cout<<i<<endl;}
void test()
{
 int arr[] = {1,5,7};
 vector v(arr,arr+3);

 for_each(v.begin(),v.end(),printInt);

}

Note that printInt can OFC be replaced with a lambda in C++0x. Also one more small variation of this usage could be (strictly for random_iterator)

 for_each(v.begin()+5,v.begin()+10,printInt);

For Fwd only iterator

 for_each(advance(v.begin(),5),advance(v.begin(),10),printInt);
3
  • How would you use this? I'm guessing you'd use a lambda for the function, but I'm not sure. Aug 25, 2011 at 5:59
  • 1
    Would tell ya, but you will have accept the answer if you think it the right way to use it. :P Kidding. Posted the example already. Aug 25, 2011 at 6:01
  • You can use a lambda here so auto range = myMultiMap.equal_range( key ); for_each( range.first, range.second, [&]( decltype( *range.first ) const& item ) { // code goes here } );
    – CashCow
    Jun 23, 2017 at 9:50
-3

You can easily generate an increasing sequence in C++11 using std::iota():

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

template<typename T>
std::vector<T> range(T start, T end)
{
  std::vector<T> r(end+1-start, T(0));
  std::iota(r.begin(), r.end(), T(start));//increasing sequence
  return r;
}

int main(int argc, const char * argv[])
{
  for(auto i:range<int>(-3,5))
    std::cout<<i<<std::endl;

  return 0;
}
2
  • 3
    The range class shall model the range. However you are literally constructing it. That is a waste of memory and memory accesses. The solution is highly redundant, because the vector holds no real information except for the number of elements and the value of first element (if it exists).
    – not-a-user
    Mar 3, 2017 at 15:12
  • Yes, this is very inefficient. Oct 30, 2017 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.